-
Notifications
You must be signed in to change notification settings - Fork 0
/
kcv.c
50 lines (35 loc) · 831 Bytes
/
kcv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Simple KeyCode Viewer for *nix terminals.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
void die(const char *s)
{
perror(s);
exit(1);
}
extern void enable_raw_mode();
//extern void disable_raw_mode();
extern void print_char_info(unsigned char);
#define DEFAULT_EXIT_KEY 'q'
int main(int argc, char** argv) {
enable_raw_mode();
char c;
int r;
char exit_key = DEFAULT_EXIT_KEY;
if(argc > 1)
exit_key = argv[1][0];
while (1) {
c = '\0';
// Non-blocking read, as c_cc[VTIME] != 0
// Note that this will not allow inputting a null character (using Ctrl-@).
r = read(STDIN_FILENO, &c, 1);
if (r == -1 && errno != EAGAIN)
die("read");
if (r == 0)
continue;
print_char_info(c);
if (c == exit_key) break;
}
return 0;
}