Linux 模拟 鼠标 键盘 事件
生活随笔
收集整理的這篇文章主要介紹了
Linux 模拟 鼠标 键盘 事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/************************************************************************* Linux 模擬 鼠標 鍵盤 事件* 說明:* 以前看到有些軟件能夠控制鼠標移動,鍵盤操作等功能,總想知道這些到底* 是怎么做到的,好像是2年前也嘗試去做這件事,但那時候對知識的匱乏直接導致* 無法進行,早上突然想到這件事,于是又搜索了一下,鑒于目前經常接觸Linux* 驅動,對這些東西的理解也就很容易。** 2016-2-27 深圳 南山平山村 曾劍鋒***********************************************************************/一、參考文章:1. 交互系統的構建之(二)Linux下鼠標和鍵盤的模擬控制http://blog.csdn.net/zouxy09/article/details/7920253
2. 在Linux中模擬擊鍵和鼠標移動http://os.51cto.com/art/201409/450283.htm
二、cat /proc/bus/input/devices:......I: Bus=0011 Vendor=0001 Product=0001 Version=ab41N: Name="AT Translated Set 2 keyboard"P: Phys=isa0060/serio0/input0S: Sysfs=/devices/platform/i8042/serio0/input/input1U: Uniq=H: Handlers=sysrq kbd event1 ---> keyboardB: PROP=0B: EV=120013B: KEY=402000000 3803078f800d001 feffffdfffefffff fffffffffffffffeB: MSC=10B: LED=7......I: Bus=0003 Vendor=0e0f Product=0003 Version=0110N: Name="VMware VMware Virtual USB Mouse"P: Phys=usb-0000:02:00.0-1/input1S: Sysfs=/devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1/2-1:1.1/input/input3U: Uniq=H: Handlers=mouse1 event3 ---> mouseB: PROP=0B: EV=17B: KEY=ffff0000 0 0 0 0B: REL=143B: MSC=10......三、權限修改:myzr@myzr:/dev/input$ ls -altotal 0drwxr-xr-x 4 root root 280 Feb 27 08:49 .drwxr-xr-x 15 root root 4260 Feb 27 08:49 ..drwxr-xr-x 2 root root 120 Feb 27 08:49 by-iddrwxr-xr-x 2 root root 180 Feb 27 08:49 by-pathcrw-r----- 1 root root 13, 64 Feb 27 08:49 event0crw-r----- 1 root root 13, 65 Feb 27 08:49 event1crw-r----- 1 root root 13, 66 Feb 27 08:49 event2crw-r----- 1 root root 13, 67 Feb 27 08:49 event3crw-r----- 1 root root 13, 68 Feb 27 08:49 event4crw-r--r-- 1 root root 13, 0 Feb 27 08:49 js0crw-r----- 1 root root 13, 63 Feb 27 08:49 micecrw-r----- 1 root root 13, 32 Feb 27 08:49 mouse0crw-r----- 1 root root 13, 33 Feb 27 08:49 mouse1crw-r----- 1 root root 13, 34 Feb 27 08:49 mouse2myzr@myzr:/dev/input$ sudo chmod 666 * 四、example code:#include <stdio.h>#include <linux/input.h>#include <fcntl.h>#include <sys/time.h>#include <unistd.h>#include <string.h>//按鍵模擬,按鍵包含按下和松開兩個環節void simulate_key(int fd, int kval){struct input_event event;gettimeofday(&event.time, 0);//按下kval鍵event.type = EV_KEY;event.value = 1;event.code = kval;write(fd, &event, sizeof(event));//同步,也就是把它報告給系統event.type = EV_SYN;event.value = 0;event.code = SYN_REPORT;write(fd, &event, sizeof(event));memset(&event, 0, sizeof(event));gettimeofday(&event.time, 0);//松開kval鍵event.type = EV_KEY;event.value = 0;event.code = kval;write(fd, &event, sizeof(event));//同步,也就是把它報告給系統event.type = EV_SYN;event.value = 0;event.code = SYN_REPORT;write(fd, &event, sizeof(event));}//鼠標移動模擬void simulate_mouse(int fd, int rel_x, int rel_y){struct input_event event;gettimeofday(&event.time, 0);//x軸坐標的相對位移event.type = EV_REL;event.value = rel_x;event.code = REL_X;write(fd, &event, sizeof(event));//y軸坐標的相對位移event.type = EV_REL;event.value = rel_y;event.code = REL_Y;write(fd, &event, sizeof(event));//同步event.type = EV_SYN;event.value = 0;event.code = SYN_REPORT;write(fd, &event, sizeof(event));}int main(int argc, char **argv){int fd_mouse = -1;int fd_kbd = -1;int i = 0;// 請保證該設備節點有寫的權限fd_kbd = open("/dev/input/event1", O_RDWR);if(fd_kbd <= 0) {printf("Can not open keyboard input file\n");return -1;}// 請保證該設備節點有寫的權限fd_mouse = open("/dev/input/event3", O_RDWR);if(fd_mouse <= 0) {printf("Can not open mouse input file\n");return -1;}for (i = 0; i < 50; i++) {//simulate_key(fd_mouse, BTN_LEFT); //模擬按下鼠標左鍵//simulate_key(fd_mouse, BTN_RIGHT); //模擬按下鼠標左鍵if (i % 3 == 0)simulate_key(fd_kbd, KEY_A); //模擬按下鍵盤A鍵//模擬鼠標相對上次x和y軸相應移動10個像素//simulate_mouse(fd_mouse, 20, 20); sleep(1);}close(fd_kbd);close(fd_mouse);}
?
總結
以上是生活随笔為你收集整理的Linux 模拟 鼠标 键盘 事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android单元測试之JUnit
- 下一篇: Delphi制作图像特殊显示效果