如何在Linux中讀取低級(jí)鼠標(biāo)單擊位置。
您可以從X11獲取初始位置,并使用相對(duì)坐標(biāo)來(lái)跟蹤指針:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <linux/input.h>#include <fcntl.h>#include <X11/Xlib.h>#define MOUSEFILE '/dev/input/event6'int main(){ int fd; struct input_event ie; display *dpy; Window root, child; int rootX, rootY, winX, winY; unsigned int mask; dpy = XOpendisplay(NULL); XQueryPointer(dpy,DefaultRootwindow(dpy),&root,&child, &rootX,&rootY,&winX,&winY,&mask); if((fd = open(MOUSEFILE, O_RDONLY)) == -1) { perror('opening device'); exit(EXIT_FAILURE); } while(read(fd, &ie, sizeof(struct input_event))) { if (ie.type == 2) { if (ie.code == 0) { rootX += ie.value; } else if (ie.code == 1) { rootY += ie.value; } printf('time%ld.%06ldtx %dty %dn', ie.time.tv_sec, ie.time.tv_usec, rootX, rootY); } else if (ie.type == 1) { if (ie.code == 272 ) { printf('Mouse button ');if (ie.value == 0) printf('released!!n');if (ie.value == 1) printf('pressed!!n'); } else {printf('time %ld.%06ldttype %dtcode %dtvalue %dn', ie.time.tv_sec, ie.time.tv_usec, ie.type, ie.code, ie.value); } } return 0;}解決方法
我正在使用此代碼從linux中的dev / input / event *讀取鼠標(biāo)事件。
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <linux/input.h>#include <fcntl.h>#define MOUSEFILE '/dev/input/event4'int main(){ int fd; struct input_event ie; if((fd = open(MOUSEFILE,O_RDONLY)) == -1) {perror('opening device');exit(EXIT_FAILURE); } while(read(fd,&ie,sizeof(struct input_event))) {printf('time %ld.%06ldttype %dtcode %dtvalue %dn',ie.time.tv_sec,ie.time.tv_usec,ie.type,ie.code,ie.value);} return 0;}
它給我的結(jié)果格式:
時(shí)間1342517261.840285類(lèi)型2代碼0值-1
“時(shí)間”是時(shí)間戳,它返回事件發(fā)生的時(shí)間。
“代碼”是事件代碼,例如REL_X或KEY_BACKSPACE,完整列表位于include / linux / input.h中。
“價(jià)值”是事件帶來(lái)的價(jià)值。EV_REL的相對(duì)更改,EV_ABS(操縱桿…)的絕對(duì)新值,或EV_KEY的釋放為0,按鍵為1以及自動(dòng)重復(fù)為2。
當(dāng)我單擊時(shí),我得到了事件,但沒(méi)有在屏幕上獲得鼠標(biāo)的位置,如何在屏幕上獲得鼠標(biāo)的位置。
編輯1:所以事實(shí)證明我必須使用相對(duì)坐標(biāo)來(lái)獲取鼠標(biāo)坐標(biāo)。我相信這是一個(gè)普遍的要求,因此可能會(huì)有庫(kù)/預(yù)先存在的代碼可用于獲取坐標(biāo)。關(guān)于該主題的任何信息將非常有用。
Edit2:解決方案
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <linux/input.h>#include <fcntl.h>#include <X11/Xlib.h>#define MOUSEFILE '/dev/input/event4'int main(){ int fd; struct input_event ie; Display *dpy; Window root,child; int rootX,rootY,winX,winY; unsigned int mask; dpy = XOpenDisplay(NULL); XQueryPointer(dpy,DefaultRootWindow(dpy),&root,&child,&rootX,&rootY,&winX,&winY,&mask); if((fd = open(MOUSEFILE,O_RDONLY)) == -1) { perror('opening device'); exit(EXIT_FAILURE); } while(read(fd,sizeof(struct input_event))) { if (ie.type == 2) { if (ie.code == 0) { XQueryPointer(dpy,&mask); //rootX += ie.value; } else if (ie.code == 1) { XQueryPointer(dpy,&mask); // rootY += ie.value; } printf('time%ld.%06ldtx %dty %dn',rootX,rootY); } else printf('time %ld.%06ldttype %dtcode %dtvalue %dn',ie.value); } return 0;}
XQueryPointer似乎是更方便的解決方案。謝謝@perreal的指導(dǎo)。
相關(guān)文章:
1. Win10無(wú)法共享打印機(jī)和文件怎么辦2. 統(tǒng)信uos怎么顯示隱藏文件? uos文件管理器隱藏文件的顯示方法3. Windows 注冊(cè)表LastKey鍵值的設(shè)置技巧4. WinXP系統(tǒng)LOL打字沒(méi)有候選框如何解決?5. Win10寬帶連接錯(cuò)誤651的原因及解決技巧6. Windows10快捷鍵失效怎么辦?這個(gè)小訣竅麻煩收好了7. Win10專(zhuān)業(yè)版必做的性能優(yōu)化!運(yùn)行速度提高90%!8. Win10經(jīng)常提示ms-gamingoverlay怎么辦?Win10經(jīng)常提示ms-gamingoverlay的解決方法9. Win10電腦硬盤(pán)容量如何查看?10. Win7旗艦版安裝程序無(wú)法創(chuàng)建新的系統(tǒng)分區(qū)怎么辦
