Skip to content

Instantly share code, notes, and snippets.

@panzi
Created September 24, 2015 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panzi/8d3233ec7ecb4cc2c27b to your computer and use it in GitHub Desktop.
Save panzi/8d3233ec7ecb4cc2c27b to your computer and use it in GitHub Desktop.
#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