/************************************************************************ * Linux Simulation Mouse Keyboard Event * Description: * Previously saw some software can control mouse movement, keyboard operation and other functions, always want to know how these exactly * how to do it, as if it was 2 years ago also tried to do this thing, but then the lack of knowledge directly led to * can not be carried out, the morning suddenly thought of this matter, and then search again, given the current contact with Linux * Drive, the understanding of these things is easy. * 2016-2-27 Shenzhen Nanshan Ping Shan village Zengjianfeng *********************************************************** ************/I. Reference article: 1. Construction of interactive System (ii) simulation of mouse and keyboard under Linux http://blog.csdn.net/zouxy09/article/details/79202532. Simulate keystrokes and mouse move http in Linux://os.51cto.com/art/201409/450283.htmSecond, Cat/proc/bus/input/Devices: ... I:bus=0011Vendor=0001product=0001version=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=0B:ev=120013B:key=4020000003803078f800d001 feffffdfffefffff Fffffffffffffffe b:msc=Tenb:led=7 ...... I:bus=0003Vendor=0e0fproduct=0003version=0110N:name="vmware VMware Virtual USB Mouse"P:phys=usb-0000: Geneva:00.0-1/input1 S:sysfs=/devices/pci0000:xx/0000:xx:11.0/0000: Geneva:00.0/usb2/2-1/2-1:1.1/input/INPUT3 U:uniq=h:handlers=MOUSE1 Event3--->Mouse B:prop=0B:ev= -B:key=ffff00000 0 0 0B:rel=143B:msc=Ten ...... Third, the right to modify: [email protected]:/dev/input$ LS-Al Total0drwxr-xr-x4Root root280Feb - ,: the . DRWXR-xr-x theRoot root4260Feb - ,: the .. DRWXR-xr-x2Root root -Feb - ,: theby-ID drwxr-xr-x2Root root theFeb - ,: theby-Path CRW-R-----1Root root -, -Feb - ,: theevent0 CRW-R-----1Root root -, $Feb - ,: theevent1 CRW-R-----1Root root -, theFeb - ,: theEvent2 CRW-R-----1Root root -, theFeb - ,: theEvent3 CRW-R-----1Root root -, theFeb - ,: theevent4 CRW-r--r--1Root root -,0Feb - ,: theJs0 CRW-R-----1Root root -, theFeb - ,: themice CRW-R-----1Root root -, +Feb - ,: themouse0 CRW-R-----1Root root -, -Feb - ,: themouse1 CRW-R-----1Root root -, theFeb - ,: themouse2 [email protected]:/dev/input$ sudo chmod666*Iv. Example code: #include<stdio.h>#include<linux/input.h>#include<fcntl.h>#include<sys/time.h>#include<unistd.h>#include<string.h>//button simulation with press and release two links voidSimulate_key (intFdintkval) { structInput_eventEvent; Gettimeofday (&Event. Time,0); //Press the Kval key Event. Type =Ev_key; Event. Value =1; Event. Code =kval; Write (FD,&Event,sizeof(Event)); //synchronization, which is to report it to the system 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); //release the Kval key Event. Type =Ev_key; Event. Value =0; Event. Code =kval; Write (FD,&Event,sizeof(Event)); //synchronization, which is to report it to the system Event. Type =Ev_syn; Event. Value =0; Event. Code =Syn_report; Write (FD,&Event,sizeof(Event)); } //Mouse Movement Simulation voidSimulate_mouse (intFdintRel_x,intrel_y) { structInput_eventEvent; Gettimeofday (&Event. Time,0); //relative displacement of x-axis coordinates Event. Type =Ev_rel; Event. Value =rel_x; Event. Code =rel_x; Write (FD,&Event,sizeof(Event)); //relative displacement of y-axis coordinates Event. Type =Ev_rel; Event. Value =rel_y; Event. Code =rel_y; Write (FD,&Event,sizeof(Event)); //Sync Event. Type =Ev_syn; Event. Value =0; Event. Code =Syn_report; Write (FD,&Event,sizeof(Event)); } intMainintargcChar**argv) { intFd_mouse =-1; intFD_KBD =-1; inti =0; //Make sure that the device node has write permissionsFD_KBD = open ("/dev/input/event1", O_RDWR); if(FD_KBD <=0) {printf ("Can not open keyboard input file\n"); return-1; } //Make sure that the device node has write permissionsFd_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 < -; i++) { //Simulate_key (Fd_mouse, btn_left); //simulate pressing the left mouse button//Simulate_key (Fd_mouse, btn_right); //simulate pressing the left mouse button ifI3==0) Simulate_key (FD_KBD, key_a); //simulate pressing the keyboard a key//simulates a mouse moving 10 pixels relative to the last X and Y axes//Simulate_mouse (Fd_mouse, 20, 20); Sleep1); } close (FD_KBD); Close (Fd_mouse); }
Linux simulates mouse keyboard events