/* keytest.c Outputs hexadecimal keycode(s) for each key pressed at the terminal Press ESC to exit the program. Written by Taylor Gurney, 2022. Public domain. */ #include #include #include #include int main(void) { struct termios t; char c[8] = {0}; ssize_t inlen = 0; int i; if (!isatty(0)) { puts("error: stdin is not a tty\n"); exit(1); } if (tcgetattr(0, &t) < 0) { puts("tcgetattr failed"); exit(1); } t.c_lflag &= ~ICANON; t.c_lflag &= ~ECHO; t.c_lflag &= ~ISIG; t.c_iflag &= ~IXOFF; t.c_iflag &= ~IXON; t.c_iflag &= ~ICRNL; if (tcsetattr(TCSADRAIN, 0, &t) < 0) { puts("tcsetattr failed"); exit(1); } puts("Press ESC to exit"); while ((inlen = read(0, c, 8)) > 0) { if (inlen == 1 && *c == '\033') { puts(""); exit(0); } for (i = 0; i < inlen; i++) { printf("%02hhx ", c[i]); fflush(stdout); } if (inlen == 1) { printf("\n%c\n", *c); fflush(stdout); } } }