Linux 事件截获2
#include <poll.h>
#include <linux/input.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>
#define OPEN_MAX 10
#define INFTIME 10
int main()
{
struct input_event event;
struct pollfd thepollfd[OPEN_MAX];
int fd;
int maxi,nready,i,n;
fd = open("/dev/input/event3", O_RDONLY);
if (fd < 0) {
perror("cannot open /dev/input/event3\n");
exit(EXIT_FAILURE);
}
thepollfd[0].fd = fd;
thepollfd[0].events = POLLRDNORM;
for(i = 1; i < OPEN_MAX; i++)
thepollfd[i].fd = -1;
maxi = 0;
for(;;) {
nready = poll(thepollfd, maxi + 1, INFTIME);
for(i = 0; i < OPEN_MAX; i++) {
if(thepollfd[i].fd < 0)
continue;
if(thepollfd[i].revents & POLLRDNORM) {
bzero(&event, sizeof(struct input_event));
if((n = read(thepollfd[i].fd, &event, sizeof(struct input_event))) >0) {
if(event.type = EV_KEY)
printf("get event type %u code %u value %d\n", event.type, event.code, event.value);
}
}
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。