Linux C编程连载【2】-鼠标

mac2022-06-30  105

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> #include <time.h> #include <errno.h> int main(void){ int fd,retval; char buf[6]; fd_set readfds,tmp_readfds; struct timeval tv; if((fd=open("/dev/input/mice",O_RDONLY|O_NONBLOCK))<0){ printf("Fail to open /dev/input/mice!\n"); return -1; } printf("Open /dev/input/mice success!\n"); tv.tv_sec=5; tv.tv_usec=0; FD_ZERO(&readfds); FD_SET(fd,&readfds); FD_SET(0,&readfds); while(FD_ISSET(fd,&readfds)){ tmp_readfds=readfds; retval=select(fd+1,&readfds,NULL,NULL,&tv); printf("retval is %d.\n",&retval); switch(retval){ case -1: { printf("Select error\n"); return -1; } break; case 0: { printf("Time out\n"); return -1; } break; default:{ printf("Select OK!\n"); if(read(fd,buf,sizeof(buf))<=0) { printf("Fail to read!\n"); continue; } printf("Button type is %d, x=%d ,y=%d\n",buf[0]&0x07,buf[1],buf[2]); } break; }//end of switch }//end of while close(fd); return 0; }

转载于:https://www.cnblogs.com/J2EEPLUS/archive/2011/09/23/2488001.html

最新回复(0)