LoveUnix » 培训认证 行业入门 » 向高手求教一个linux读取鼠标状态问题
让LU留住您的每

一天 让LU博客留住您的每一天
2005-1-19 10:49 binbinyouli
程序想要实现在LINUX控制台下面,用SELECT不断检测鼠标状态,如果发生鼠标事件就用read函数读取鼠标的位移和按键,但是发现读取的速度比鼠标移动慢多了,请问是怎么一回事??请高手赐教代码如下:<br />#include &lt;stdio.h&gt;<br />#include &lt;sys/time.h&gt;<br />#include &lt;sys/types.h&gt;<br />#include &lt;unistd.h&gt;<br />#include &lt;sys/stat.h&gt; <br />#include &lt;fcntl.h&gt; <br />#include &lt;sys/select.h&gt;<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&amp;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(&quot;/dev/mouse&quot;,O_RDWR);<br />                FD_ZERO(&amp;fd_read);<br />                FD_CLR(testmouse, &amp;fd_read); <br /> <br />                FD_SET(testmouse, &amp;fd_read);                 <br />        <br />        switch (select(testmouse+1, &amp;fd_read,NULL,NULL,&amp;tv)) <br />        { <br />                case -1: <br />                //error handled by u; <br />                printf(&quot;system device err\n&quot;);<br />                break;<br />                case 0: <br />                //timeout hanled by u; <br />        //        printf(&quot;time is out\n&quot;);<br />                break;<br />                default: <br />                if (FD_ISSET(testmouse,&amp;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(&quot;shift x is %d\n&quot;,buf[1]); //print mouse state        <br />                        printf(&quot;shift y is %d\n&quot;,buf[2]); //print mouse state<br />                printf(&quot;button pressed value is %d and %d\n&quot;,buf[0],buf[3]);<br />                printf(&quot;position x is %d\n&quot;,position[1]); //print mouse position<br />                printf(&quot;position y is %d\n&quot;,position[2]); //print mouse position<br />        <br />                <br />        <br />        <br />                <br /><br />                }<br />        }<br />        close(testmouse);        <br />        }<br />        printf(&quot;mouse test program is termination\n&quot;);<br />        return 0;<br />        <br />}

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.