Created
September 24, 2015 21:11
-
-
Save panzi/8d3233ec7ecb4cc2c27b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <linux/input.h> | |
#include <sys/types.h> | |
#include <fcntl.h> | |
int main (int argc, char *argv[]) { | |
struct input_event ev; | |
int fd = -1; | |
const char *device = NULL; | |
int status = 0; | |
if (argc < 2) { | |
fprintf(stderr, "error: no keyboard device given\n"); | |
goto error; | |
} | |
device = argv[1]; | |
fd = open(device, O_RDONLY); | |
if (fd < 0) { | |
perror(device); | |
goto error; | |
} | |
for (;;) { | |
ssize_t count = read(fd, &ev, sizeof(ev)); | |
if (count != sizeof(ev)) { | |
perror(device); | |
goto error; | |
} | |
if (ev.type == EV_KEY && ev.value == 1) { | |
printf("%d\n", ev.code); | |
fflush(stdout); | |
} | |
} | |
goto end; | |
error: | |
status = 1; | |
end: | |
if (fd != -1) { | |
close(fd); | |
} | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment