/************************************************************************
* 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= Vendor= Product= 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=
B: EV=
B: KEY= 3803078f800d001 feffffdfffefffff fffffffffffffffe
B: MSC=
B: LED=
......
I: Bus= Vendor=0e0f Product= Version=
N: Name="VMware VMware Virtual USB Mouse"
P: Phys=usb-::00.0-/input1
S: Sysfs=/devices/pci0000:/::11.0/::00.0/usb2/-/-:1.1/input/input3
U: Uniq=
H: Handlers=mouse1 event3 ---> mouse
B: PROP=
B: EV=
B: KEY=ffff0000
B: REL=
B: MSC=
...... 三、权限修改:
myzr@myzr:/dev/input$ ls -al
total
drwxr-xr-x root root Feb : .
drwxr-xr-x root root Feb : ..
drwxr-xr-x root root Feb : by-id
drwxr-xr-x root root Feb : by-path
crw-r----- root root , Feb : event0
crw-r----- root root , Feb : event1
crw-r----- root root , Feb : event2
crw-r----- root root , Feb : event3
crw-r----- root root , Feb : event4
crw-r--r-- root root , Feb : js0
crw-r----- root root , Feb : mice
crw-r----- root root , Feb : mouse0
crw-r----- root root , Feb : mouse1
crw-r----- root root , Feb : mouse2
myzr@myzr:/dev/input$ sudo chmod * 四、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, ); //按下kval键
event.type = EV_KEY;
event.value = ;
event.code = kval;
write(fd, &event, sizeof(event)); //同步,也就是把它报告给系统
event.type = EV_SYN;
event.value = ;
event.code = SYN_REPORT;
write(fd, &event, sizeof(event)); memset(&event, , sizeof(event));
gettimeofday(&event.time, ); //松开kval键
event.type = EV_KEY;
event.value = ;
event.code = kval;
write(fd, &event, sizeof(event)); //同步,也就是把它报告给系统
event.type = EV_SYN;
event.value = ;
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, ); //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 = ;
event.code = SYN_REPORT;
write(fd, &event, sizeof(event));
} int main(int argc, char **argv)
{
int fd_mouse = -;
int fd_kbd = -;
int i = ; // 请保证该设备节点有写的权限
fd_kbd = open("/dev/input/event1", O_RDWR);
if(fd_kbd <= ) {
printf("Can not open keyboard input file\n");
return -;
} // 请保证该设备节点有写的权限
fd_mouse = open("/dev/input/event3", O_RDWR);
if(fd_mouse <= ) {
printf("Can not open mouse input file\n");
return -;
} for (i = ; i < ; i++) {
//simulate_key(fd_mouse, BTN_LEFT); //模拟按下鼠标左键
//simulate_key(fd_mouse, BTN_RIGHT); //模拟按下鼠标左键
if (i % == )
simulate_key(fd_kbd, KEY_A); //模拟按下键盘A键
//模拟鼠标相对上次x和y轴相应移动10个像素
//simulate_mouse(fd_mouse, 20, 20);
sleep();
} close(fd_kbd);
close(fd_mouse);
}