]> git.scottworley.com Git - keystroke-timestamps/blob - keystroke-timestamps.c
2ab4c42938a2cb3177bd1f00c1a2f413d8ba712c
[keystroke-timestamps] / keystroke-timestamps.c
1 #include <err.h>
2 #include <fcntl.h>
3 #include <getopt.h>
4 #include <linux/input.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sysexits.h>
9 #include <unistd.h>
10
11 int main (int argc, char **argv)
12 {
13 int print_usec = 0;
14 struct option long_options[] = {
15 {"usec", no_argument, &print_usec, 1 },
16 {0, 0, 0, 0 },
17 };
18 while (1) {
19 int c = getopt_long(argc, argv, "", long_options, NULL);
20 if (c == -1) break;
21 if (c != 0) exit(EX_USAGE);
22 }
23
24 int fd = open("/dev/input/by-path/platform-i8042-serio-0-event-kbd", O_RDONLY);
25 if (fd < 0) {
26 err(EX_NOINPUT, "Could not open keyboard event file");
27 }
28 struct input_event i;
29 while (read(fd, &i, sizeof(i)) == sizeof(i)) {
30 if (i.type == 1 && i.value == 1) {
31 if (print_usec) {
32 printf("%ld.%ld\n", i.time.tv_sec, i.time.tv_usec);
33 } else {
34 printf("%ld\n", i.time.tv_sec);
35 }
36 fflush(stdout);
37 }
38 }
39 return 0;
40 }