日期:2014-05-16  浏览次数:20636 次

Linux 事件截获2
#include <stdio.h>
#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);
                }
            }
        }
    }
}