生活随笔
收集整理的這篇文章主要介紹了
Linux之解析鼠标input事件数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Linux中USB鼠標驅動:http://blog.csdn.net/qq_21792169/article/details/48790745
或者直接Linux自帶的USB鼠標驅動。
/* ?
? ? ?* 向輸入子系統匯報鼠標事件情況,以便作出反應。 ?
? ? ?* data 數組的第0個字節:bit 0、1、2、3、4分別代表左、右、中、SIDE、EXTRA鍵的按下情況; ?
? ? ?* data 數組的第1個字節:表示鼠標的水平位移; ?
? ? ?* data 數組的第2個字節:表示鼠標的垂直位移; ?
? ? ?* data 數組的第3個字節:REL_WHEEL位移。 ?
? ? ?* data 數組的第4個字節:中間滾輪事件。 ?
? ? ?*/ ?
? ? input_report_key(dev, BTN_LEFT, ? data[0] & 0x01); ??
? ? input_report_key(dev, BTN_RIGHT, ?data[0] & 0x02); ??
? ? input_report_key(dev, BTN_MIDDLE, data[0] & 0x04); ??
? ? input_report_key(dev, BTN_SIDE, ? data[0] & 0x08); ??
? ? input_report_key(dev, BTN_EXTRA, ?data[0] & 0x10); ??
? ? input_report_rel(dev, REL_X, ? ? data[1]); ??
? ? input_report_rel(dev, REL_Y, ? ? data[2]); ??
? ? input_report_rel(dev, REL_WHEEL, data[3]); ??
當我們寫好鼠標驅動程序后,我們測試利用的是cat ?/dev/event0 ? 或者hexdump ? ?/dev/event0 ?有些事在/dev/input/event* ? /dev/input/mice
這是Input輸入事件的上報事件的格式
struct timeval {
__kernel_time_ttv_sec;/* seconds */
__kernel_suseconds_ttv_usec;/* microseconds */
};
/*
?* The event structure itself
?*/
struct input_event {
struct timeval time; ? ? ? ? ?/* 固定格式,一定要理解了 */
__u16 type;
__u16 code;
__s32 value;
};
這是hexdump /dev/event0 ?后的數據:(非常重要的知識點)
? ? ? ? ? ? ? ? ? ? ? ?秒 ? ? ? ?微秒 ? ? ?類 ? ? ? ? ? code ? ?value
0000000 0bb2 0000 0e48 000c 0001 0026 0001 0000 ? ? ? ? ? ?
0000010 0bb2 0000 0e54 000c 0000 0000 0000 0000
0000020 0bb2 0000 5815 000e 0001 0026 0000 0000
0000030 0bb2 0000 581f 000e 0000 0000 0000 0000 ?
/* 第一個字符串代表系統時間,無影響,第二和第三個字符串代表秒,第四和第五個字符串代表微妙,依次是類,類下面的那種事件,最后32位是具體值,從上面結構體我們是可以看出來的,這里要注意我們讀取數據時候第一個字符串是不會讀取的,我就犯了第一個錯誤,第二個錯誤我們讀取數據應該從右往左讀數,這滿足于小端模式*/
現在開始上代碼:
[html]?view plain
?copy#include?<sys/types.h>??#include?<sys/stat.h>??#include?<fcntl.h>??#include?<stdio.h>??#include?<unistd.h>??#include?<string.h>??#include?<linux/input.h>????#define?DEV_NAME???"/dev/event2"????#define?DBG_PRINTF?printf??//#define?DBG_PRINTF(...)????struct?input_event?input_mouse;????int?main(int?argc,?char?**argv)??{??????int?fd,retval;??????fd_set?readfds;??????fd?=?open(DEV_NAME,?O_RDONLY);??????if?(fd?<?0)??????{??????????printf("can't?open?%s\n",DEV_NAME);??????????return?-1;??????}??????????while(1)??{??????FD_ZERO(?&readfds?);????????FD_SET(?fd,?&readfds?);??????????retval?=?select(?fd+1,?&readfds,?NULL,?NULL,?NULL);????????if(retval==0)???????{????????????printf(?"Time?out!\n"?);????????}????????if(FD_ISSET(fd,&readfds))???????{????????????read(fd,?&input_mouse,sizeof(struct?input_event));???????????switch(input_mouse.type)??????????{??????????????case?EV_KEY:??????????????{/*?have?key?is?press?*/??????????????????switch(input_mouse.code)??????????????????{??????????????????????case?BTN_LEFT:??????????????????????{??????????????????????????if(input_mouse.value==1)??????????????????????????????DBG_PRINTF("the?left?is?press!\n");??????????????????????}??????????????????????break;??????????????????????case?BTN_RIGHT:??????????????????????{??????????????????????????if(input_mouse.value==1)??????????????????????????????DBG_PRINTF("the?right?is?press!\n");??????????????????????????}??????????????????????break;??????????????????????case?BTN_MIDDLE:??????????????????????{??????????????????????????if(input_mouse.value==1)??????????????????????????????DBG_PRINTF("the?middle?is?press!\n");??????????????????????}??????????????????????break;????????????????????????????????????}??????????????}???????????????break;??????????????case?EV_REL:??????????????{??????????????????switch(input_mouse.code)??????????????????{??????????????????????case?REL_X:??????????????????????{??????????????????????????if(input_mouse.value>0)??????????????????????????????DBG_PRINTF("X?slip?is?right!\n");??????????????????????????????else?if(input_mouse.value<0)??????????????????????????????????DBG_PRINTF("X?slip?is?left!\n");??????????????????????}??????????????????????break;??????????????????????case?REL_Y:??????????????????????{??????????????????????????if(input_mouse.value<0)??????????????????????????????DBG_PRINTF("Y?slip?is?up!\n");??????????????????????????????else?if(input_mouse.value>0)??????????????????????????????????DBG_PRINTF("Y?slip?is?down!\n");??????????????????????}??????????????????????break;??????????????????}????????????????????????????}??????????????break;??????????}??????}?????}??close(fd);??return?0;??}??
常見的事件類型:
事件類型下常見的值:定義在include/linux/input/h中
總結
以上是生活随笔為你收集整理的Linux之解析鼠标input事件数据的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。