2005-1-19 10:49
binbinyouli
程序想要实现在LINUX控制台下面,用SELECT不断检测鼠标状态,如果发生鼠标事件就用read函数读取鼠标的位移和按键,但是发现读取的速度比鼠标移动慢多了,请问是怎么一回事??请高手赐教代码如下:<br />#include <stdio.h><br />#include <sys/time.h><br />#include <sys/types.h><br />#include <unistd.h><br />#include <sys/stat.h> <br />#include <fcntl.h> <br />#include <sys/select.h><br /><br />int main()<br />{<br /> int i=0;<br /> char inputchar;<br /> int testmouse;<br /> char buf[50];//mouse parameter <br /> char position[4]={0,0,0,0};//store mouse position value<br /> //buffer[0]:shift x(-127,127);buffer[1]: y(-127,127) ;<br /> //buffer[2]: button (0x80,0x80+the buttons currently down) for example left button is 0x81, right is 0x84, left&right is 0x85;<br /> <br /> fd_set fd_read; <br /> struct timeval tv; <br /> tv.tv_sec = 5;<br /> tv.tv_usec=0;<br /><br /> <br /> for(;;) <br /> { <br /> testmouse = open("/dev/mouse",O_RDWR);<br /> FD_ZERO(&fd_read);<br /> FD_CLR(testmouse, &fd_read); <br /> <br /> FD_SET(testmouse, &fd_read); <br /> <br /> switch (select(testmouse+1, &fd_read,NULL,NULL,&tv)) <br /> { <br /> case -1: <br /> //error handled by u; <br /> printf("system device err\n");<br /> break;<br /> case 0: <br /> //timeout hanled by u; <br /> // printf("time is out\n");<br /> break;<br /> default: <br /> if (FD_ISSET(testmouse,&fd_read)) <br /> // now u read or recv something; <br /> <br /> read(testmouse,buf,50); <br /> <br /> position[0]=position[0];<br /> position[1]=position[1]+buf[1];<br /> position[2]=position[2]+buf[2];<br /> position[3]=position[3];<br /><br /> <br /> <br /> printf("shift x is %d\n",buf[1]); //print mouse state <br /> printf("shift y is %d\n",buf[2]); //print mouse state<br /> printf("button pressed value is %d and %d\n",buf[0],buf[3]);<br /> printf("position x is %d\n",position[1]); //print mouse position<br /> printf("position y is %d\n",position[2]); //print mouse position<br /> <br /> <br /> <br /> <br /> <br /><br /> }<br /> }<br /> close(testmouse); <br /> }<br /> printf("mouse test program is termination\n");<br /> return 0;<br /> <br />}