Ask and ye shall receive.
I have cracked reading keypresses from the cheap ($30) three pedal usb keyboard thing…
Stripped of error checking, includes, and such cruft here is how: Three steps:
- Wait for a event on the input file handle
select
- Check what key has been pressed
ioctl/EVIOCGKEY
- Consume all the data ready to be read from the file handle so next time around the loop
select
can do its thing.
So simple once you know how…
fd_set rfds;
struct timeval tv;
int retval, res, fd;
unsigned buff_size = 1024;
char buf[buff_size];
unsigned yalv;
uint8_t key_b[KEY_MAX/8 + 1];
fd = open("/dev/input/event0", O_RDONLY);
last_yalv = 0;
while(1){
// Wait for something to happen on the file
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
retval = select(fd+1, &rfds, NULL, NULL, &tv);
// What key was activated?
memset(key_b, 0, sizeof(key_b));
if(ioctl(fd, EVIOCGKEY(sizeof(key_b)), key_b) == -1){
printf("IOCTL Error %s\n", strerror(errno));
return -1;
}
for (yalv = 0; yalv < KEY_MAX; yalv++) {
if (test_bit(yalv, key_b)) {
printf(" This is the key 0x%02x\n", yalv);
}
}
/* Consume what can be read from fd */
res = read(fd, &buf, buff_size);
}