1 /* vi: set sw=4 ts=4: */
3 * shows keys pressed. inspired by kbd package
5 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
9 //config:config SHOWKEY
10 //config: bool "showkey (4.7 kb)"
12 //config: select PLATFORM_LINUX
14 //config: Shows keys pressed.
16 //applet:IF_SHOWKEY(APPLET(showkey, BB_DIR_USR_BIN, BB_SUID_DROP))
18 //kbuild:lib-$(CONFIG_SHOWKEY) += showkey.o
20 //usage:#define showkey_trivial_usage
21 //usage: "[-a | -k | -s]"
22 //usage:#define showkey_full_usage "\n\n"
23 //usage: "Show keys pressed\n"
24 //usage: "\n -a Display decimal/octal/hex values of the keys"
25 //usage: "\n -k Display interpreted keycodes (default)"
26 //usage: "\n -s Display raw scan-codes"
34 struct termios tio, tio0;
36 #define G (*ptr_to_globals)
37 #define kbmode (G.kbmode)
40 #define INIT_G() do { \
41 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
46 // also used by microcom
48 static void xget1(struct termios *t, struct termios *oldt)
50 tcgetattr(STDIN_FILENO, oldt);
55 static void xset1(struct termios *t)
57 int ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, t);
59 bb_perror_msg("can't tcsetattr for stdin");
63 int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
64 int showkey_main(int argc UNUSED_PARAM, char **argv)
67 OPT_a = (1<<0), // display the decimal/octal/hex values of the keys
68 OPT_k = (1<<1), // display only the interpreted keycodes (default)
69 OPT_s = (1<<2), // display only the raw scan-codes
74 // FIXME: aks are all mutually exclusive
75 getopt32(argv, "aks");
77 // prepare for raw mode
79 // put stdin in raw mode
82 #define press_keys "Press any keys, program terminates %s:\r\n\n"
84 if (option_mask32 & OPT_a) {
85 // just read stdin char by char
88 printf(press_keys, "on EOF (ctrl-D)");
90 // read and show byte values
91 while (1 == read(STDIN_FILENO, &c, 1)) {
92 printf("%3u 0%03o 0x%02x\r\n", c, c, c);
93 if (04 /*CTRL-D*/ == c)
97 // we assume a PC keyboard
98 xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
99 printf("Keyboard mode was %s.\r\n\n",
100 kbmode == K_RAW ? "RAW" :
101 (kbmode == K_XLATE ? "XLATE" :
102 (kbmode == K_MEDIUMRAW ? "MEDIUMRAW" :
103 (kbmode == K_UNICODE ? "UNICODE" : "UNKNOWN")))
106 // set raw keyboard mode
107 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
109 // we should exit on any signal; signals should interrupt read
110 bb_signals_recursive_norestart(BB_FATAL_SIGS, record_signo);
112 // inform user that program ends after time of inactivity
113 printf(press_keys, "10s after last keypress");
115 // read and show scancodes
116 while (!bb_got_signal) {
120 // setup 10s watchdog
124 n = read(STDIN_FILENO, buf, sizeof(buf));
127 if (option_mask32 & OPT_s) {
128 // show raw scancodes
129 printf("0x%02x ", buf[i++]);
131 // show interpreted scancodes (default)
136 && (buf[i+1] & 0x80) != 0
137 && (buf[i+2] & 0x80) != 0
139 kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
145 printf("keycode %3u %s", kc, (c & 0x80) ? "release" : "press");
151 // restore keyboard mode
152 xioctl(STDIN_FILENO, KDSKBMODE, (void *)(ptrdiff_t)kbmode);
155 // restore console settings