kst/ke/keypress.c

50 lines
677 B
C
Raw Normal View History

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
2020-02-18 22:31:23 +00:00
#include <wctype.h>
struct termios orig_termios;
void
disableRawMode()
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
}
void
enableRawMode()
{
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
int
main()
{
enableRawMode();
2020-02-18 22:31:23 +00:00
wchar_t c;
while (read(STDIN_FILENO, &c, sizeof(c)) > 0 && c != 'q') {
if (iswcntrl(c)) {
printf("$%02x\n", c);
} else {
2020-02-18 22:31:23 +00:00
printf("$%02x ('%lc')\n", c, c);
}
}
2020-02-18 22:31:23 +00:00
perror("read");
return 0;
}