8
#include <linux/keyboard.h>
14
int tmp; /* for debugging */
21
* version 0.81 of showkey would restore kbmode unconditially to XLATE,
22
* thus making the console unusable when it was called under X.
28
if (ioctl(fd, KDGKBMODE, &oldkbmode)) {
38
m = "MEDIUMRAW"; break;
42
m = _("?UNKNOWN?"); break;
44
printf(_("kb mode was %s\n"), m);
45
if (oldkbmode != K_XLATE) {
46
printf(_("[ if you are trying this under X, it might not work\n"
47
"since the X server is also reading /dev/console ]\n"));
54
if (ioctl(fd, KDSKBMODE, oldkbmode)) {
58
if (tcsetattr(fd, 0, &old) == -1)
65
printf(_("caught signal %d, cleaning up...\n"), x);
79
"showkey version %s\n\n"
80
"usage: showkey [options...]\n"
82
"valid options are:\n"
84
" -h --help display this help text\n"
85
" -a --ascii display the decimal/octal/hex values of the keys\n"
86
" -s --scancodes display only the raw scan-codes\n"
87
" -k --keycodes display only the interpreted keycodes (default)\n"
93
main (int argc, char *argv[]) {
94
const char *short_opts = "haskV";
95
const struct option long_opts[] = {
96
{ "help", no_argument, NULL, 'h' },
97
{ "ascii", no_argument, NULL, 'a' },
98
{ "scancodes", no_argument, NULL, 's' },
99
{ "keycodes", no_argument, NULL, 'k' },
100
{ "version", no_argument, NULL, 'V' },
104
int show_keycodes = 1;
108
unsigned char buf[16];
111
set_progname(argv[0]);
113
setlocale(LC_ALL, "");
114
bindtextdomain(PACKAGE, LOCALEDIR);
117
while ((c = getopt_long(argc, argv,
118
short_opts, long_opts, NULL)) != -1) {
130
print_version_and_exit();
141
/* no mode and signal and timer stuff - just read stdin */
144
if (tcgetattr(fd, &old) == -1)
146
if (tcgetattr(fd, &new) == -1)
149
new.c_lflag &= ~ (ICANON | ISIG);
150
new.c_lflag |= (ECHO | ECHOCTL);
155
if (tcsetattr(fd, TCSAFLUSH, &new) == -1)
157
printf(_("\nPress any keys - "
158
"Ctrl-D will terminate this program\n\n"));
161
n = read(fd, buf, 1);
163
printf(" \t%3d 0%03o 0x%02x\n",
164
buf[0], buf[0], buf[0]);
165
if (n != 1 || buf[0] == 04)
169
if (tcsetattr(fd, 0, &old) == -1)
177
/* the program terminates when there is no input for 10 secs */
178
signal(SIGALRM, watch_dog);
181
if we receive a signal, we want to exit nicely, in
182
order not to leave the keyboard in an unusable mode
186
signal(SIGQUIT, die);
188
signal(SIGTRAP, die);
189
signal(SIGABRT, die);
192
signal(SIGKILL, die);
193
signal(SIGUSR1, die);
194
signal(SIGSEGV, die);
195
signal(SIGUSR2, die);
196
signal(SIGPIPE, die);
197
signal(SIGTERM, die);
199
signal(SIGSTKFLT, die);
201
signal(SIGCHLD, die);
202
signal(SIGCONT, die);
203
signal(SIGSTOP, die);
204
signal(SIGTSTP, die);
205
signal(SIGTTIN, die);
206
signal(SIGTTOU, die);
209
if (tcgetattr(fd, &old) == -1)
211
if (tcgetattr(fd, &new) == -1)
214
new.c_lflag &= ~ (ICANON | ECHO | ISIG);
216
new.c_cc[VMIN] = sizeof(buf);
217
new.c_cc[VTIME] = 1; /* 0.1 sec intercharacter timeout */
219
if (tcsetattr(fd, TCSAFLUSH, &new) == -1)
221
if (ioctl(fd, KDSKBMODE, show_keycodes ? K_MEDIUMRAW : K_RAW)) {
226
printf(_("press any key (program terminates 10s after last keypress)...\n"));
229
n = read(fd, buf, sizeof(buf));
230
for (i = 0; i < n; i++) {
232
printf("0x%02x ", buf[i]);
234
printf(_("keycode %3d %s\n"),
236
buf[i] & 0x80 ? _("release")