-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowKeyCodes
More file actions
executable file
·167 lines (117 loc) · 3.89 KB
/
showKeyCodes
File metadata and controls
executable file
·167 lines (117 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env perl -w
#
# showKeyCodes: show what the user typed, from Perl's POV.
#
# Written by Steven J. DeRose, 2012-04-30.
# 2016-09-23: Print controls as pictures instead of selves.
#
# To do:
#
use strict;
use Getopt::Long;
use Term::ReadKey;
#use sjdUtils;
Term::ReadKey::ReadMode 1;
our $VERSION_DATE = "2016-09-23";
# General options
#
my %args = (
"quiet" => 0,
"verbose" => 0,
);
binmode(STDOUT, ":encoding(utf8)");
###############################################################################
# Process options
#
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"h|help" => sub { system "perldoc $0"; exit; },
"q|quiet!" => sub { $args{quiet} = $_[1]; },
"v|verbose+" => sub { $args{verbose} = $_[1]; },
"version" => sub {
dieCleanly("Version of $VERSION_DATE, by Steven J. DeRose.\n");
},
);
($result) || dieCleanly("Bad options.\n");
###############################################################################
# Validate and default options
#
my %options = ();
if ($args{interact}) {
%options = %args;
}
###############################################################################
#
Term::ReadKey::ReadMode 3;
my $prompt = "\n>";
print $prompt;
my $lasttime = time();
my $printed = 1;
my $c = "";
while (1) {
while (not defined ($c = Term::ReadKey::ReadKey(-1))) {
if ($lasttime != time()) {
if (!$printed) {
print $prompt;
$printed = 1;
}
$lasttime = time();
}
}
$printed = 0;
my $o = ord($c);
my $cDisplay = ($o<32) ? chr(0x2400+$o) : $c;
print sprintf("\n Got '%s' (d%d, x%x)", $cDisplay, $o, $o);
if ($c eq 'q') { last; }
}
Term::ReadKey::ReadMode 1;
($args{"quiet"}) || print("\nDone.");
exit;
###############################################################################
###############################################################################
# Be sure to reset terminal before exiting.
#
sub dieCleanly {
my ($msg) = @_;
Term::ReadKey::ReadMode 1;
die $msg;
}
###############################################################################
###############################################################################
###############################################################################
#
=pod
=head1 Usage
showKeyCodes: Echo the codes for whatever keys the user hits.
Some keys produce multi-byte sequences. All the bytes will be
listed together; then there will be a slight pause before you are
prompted for the next key. If you hit another key before the prompt,
it will show up with the preceding group (because it was easy that way).
If C0 control characters occur, they will be display as Unicode
"Control Pictures" (U+2400 and following), which are tiny renditions
of the control-character mnemonics, such as ESC.
If the script is terminated abnormally, it may not be able to
reset the terminal to its normal mode. In that case, use the *nix C<reset>
command.
=head1 Options (use '-no...' to negate when applicable)
=over
=item * B<--quiet> OR B<-q>
Suppress most messages, and line-by-line reporting.
=item * B<--verbose> OR B<-v>
Add more detailed messages (repeatable).
=item * B<--version>
Show version information and exit.
=back
=head1 Related commands
C<showkey -a> does essentially the same thing, I just didn't find it
before writing this.
C<tput>, C<showInvisibles>, C<terminfo2xml>.
=head1 Known bugs and limitations
Some keys get intercepted (for example by Gnome) before this script
ever sees them.
=head1 Ownership
This work by Steven J. DeRose is licensed under a Creative Commons
Attribution-Share Alike 3.0 Unported License. For further information on
this license, see L<http://creativecommons.org/licenses/by-sa/3.0/>.
For the most recent version, see L<http://www.derose.net/steve/utilities/>.
=cut