/************************************************************************ * 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=ab41 N: Name="AT Translated Set 2 keyboard" P: Phys=isa0060/serio0/input0 S: Sysfs=/devices/platform/i8042/serio0/input/input1 U: Uniq= H: Handlers=sysrq kbd event1 ---> keyboard B: PROP=0 B: EV=120013 B: KEY=402000000 3803078f800d001 feffffdfffefffff fffffffffffffffe B: MSC=10 B: LED=7 ...... I: Bus=0003 Vendor=0e0f Product=0003 Version=0110 N: Name="VMware VMware Virtual USB Mouse" P: Phys=usb-0000:02:00.0-1/input1 S: Sysfs=/devices/pci0000:00/0000:00:11.0/0000:02:00.0/usb2/2-1/2-1:1.1/input/input3 U: Uniq= H: Handlers=mouse1 event3 ---> mouse B: PROP=0 B: EV=17 B: KEY=ffff0000 0 0 0 0 B: REL=143 B: MSC=10 ...... 三、權限修改: myzr@myzr:/dev/input$ ls -al total 0 drwxr-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-id drwxr-xr-x 2 root root 180 Feb 27 08:49 by-path crw-r----- 1 root root 13, 64 Feb 27 08:49 event0 crw-r----- 1 root root 13, 65 Feb 27 08:49 event1 crw-r----- 1 root root 13, 66 Feb 27 08:49 event2 crw-r----- 1 root root 13, 67 Feb 27 08:49 event3 crw-r----- 1 root root 13, 68 Feb 27 08:49 event4 crw-r--r-- 1 root root 13, 0 Feb 27 08:49 js0 crw-r----- 1 root root 13, 63 Feb 27 08:49 mice crw-r----- 1 root root 13, 32 Feb 27 08:49 mouse0 crw-r----- 1 root root 13, 33 Feb 27 08:49 mouse1 crw-r----- 1 root root 13, 34 Feb 27 08:49 mouse2 myzr@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); }