From: Eric S. Raymond Date: Mon, 21 Oct 1996 18:00:52 +0000 (+0000) Subject: Initial revision X-Git-Tag: 1.1~1 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=6c9657edf042b9361391aa2b5f744c3b12d50707;p=showkey.git Initial revision --- diff --git a/showkey.c b/showkey.c new file mode 100644 index 0000000..abbfd71 --- /dev/null +++ b/showkey.c @@ -0,0 +1,87 @@ +/* + * showkey.c -- display cooked key sequences + * + * Invoke this (no arguments needed) to see keycap-to-keystrokes mappings. + * + * by Eric S. Raymond , 1 Nov 88 + */ +#include +#include +#include + +#define META 0x80 +#define ESC 0x1b + +static char *lowchars[] = +{ + "NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", + "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", + "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", + "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US", + "SP", +}; + +static int signalled; + +static void catcher(sig) +int sig; +{ + signalled = sig; +} + +main() +{ + struct termio cooked, raw; + unsigned int c, timeouts; + + for (c = SIGHUP; c <= SIGPOLL; c++) + (void) signal(c, catcher); + + (void) ioctl(0, TCGETA, &cooked); + + (void) memcpy(&raw, &cooked, sizeof(struct termio)); + raw.c_lflag &=~ (ICANON | ECHO); + raw.c_cc[VEOL] = 1; + raw.c_cc[VEOF] = 2; + (void) ioctl(0, TCSETA, &raw); + + (void) printf("Type any key to see the sequence it sends, DEL to quit.\n"); + signalled = 0; + timeouts = 1; + while (!signalled) + { + int meta; + + if (read(0, &c, 1) != 0) + timeouts = 0; + else + /* + * This is supposed to cause the program to emit a newline .2 sec + * after reading a key sequence, but it doesn't work, I don't know + * why. + */ + { + if (timeouts++ == 0) + (void) putchar('\n'); + continue; + } + + if (meta = (c & META)) + c &=~ META; + + if (c <= ' ') + (void) printf("<%s%s>", meta ? "ALT-" : "", lowchars[c]); + else if (c == 0x3F) + (void) printf("<%sDEL>", meta ? "ALT-" : ""); + else if (meta) + (void) printf("", c); + else + (void) putchar(c); + (void) fflush(stdout); + } + + (void) printf("\nBye...\n"); + (void) ioctl(0, TCSETA, &cooked); +} + +/* showkey.c ends here */