#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <linux/input.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
struct input_event event;
int inputfd;
int maxfdp1;
inputfd = open("/dev/input/event3", O_RDONLY);
if(inputfd < 0){
perror("cannot open /dev/input/event3\n");
return EXIT_FAILURE;
}
fd_set rset;
FD_ZERO(&rset);
for(;;) {
printf("loop begin\n");
FD_SET(inputfd, &rset);
maxfdp1 = inputfd +1;
if(select(maxfdp1, &rset, NULL, NULL, NULL) < 0) {
perror("select error\n");
break;
}
//printf("select ok get file fd \n");
if(FD_ISSET(inputfd, &rset)) {
// printf("inputfd is ok\n");
if(read(inputfd, &event, sizeof(struct input_event)) == 0) {
perror("read event error\n");
break;
}
// printf("read ok\n");
if (event.type == EV_KEY) {
printf("get event type %u code %u value %d\n", event.type, event.code, event.value);
}
}
printf("loop end\n");
}
}