struct input_event结构体是linux输入子系统的向应用层传递的接口。操作/dev/input/event1文件,写入个input_event结构体就可以模拟按键的输入。
#inlcude <linux/input.h>
struct input_event {
struct timeval time;//按键时间
__u16 type;//类型
__u16 code;//需模拟成的按键
__s32 value;//按下或释放
};
type:
#define EV_SYN 0x00
#define EV_KEY 0x01 //按键
#define EV_REL 0x02 //相对坐标(轨迹球)
#define EV_ABS 0x03 //绝对坐标
#define EV_MSC 0x04 //其他
#define EV_SW 0x05
#define EV_LED 0x11 //LED
#define EV_SND 0x12//声音
#define EV_REP 0x14//repeat
#define EV_FF 0x15
#define EV_PWR 0x16
#define EV_FF_STATUS 0x17
#define EV_MAX 0x1f
#define EV_CNT (EV_MAX+1)
code:
事件类型为EV_KEY时,该code为设备按键代码
0~127为键盘上的按键代码,
0x110~0x116 为鼠标上按键代码, 其中0x110(BTN_ LEFT)为鼠标左键, 0x111(BTN_RIGHT)为鼠标右键, 0x112(BTN_ MIDDLE)为鼠标中键.
其它代码含义请参看include/linux /input.h文件.
事件的类型代码为EV_REL,该code为轨迹的类型.
0x00:指示鼠标的X轴方向 REL_X
0x01:指示鼠标的Y轴方向REL_Y
0x08:指示鼠标中轮子方向REL_WHEEL
value:
事件的类型代码是EV_KEY时,按下值为1,释放值为0
事件的类型代码是EV_ REL时,value的正数值和负数值分别代表两个不同方向的值.
————————————————
版权声明:本文为CSDN博主「holyjw」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/holyjw/article/details/89881964
………………………………………………
示例:编写一个服务进程mousemove,每隔10s移动一次鼠标光标,使x值增加50,y值保持不变。
main.c:
int main(int argc, char *argv[])
{
int fd;
struct input_event event;
if (argc != 2)
{
syslog (LOG_ERR,"No device specified");
}
fd = open(argv[1], O_RDWR);
if (fd < 0)
{
syslog(LOG_ERR, "Failed to open device %s",argv[1]);
exit(1);
}
while (1)
{
event.type = EV_REL;
event.code = REL_X;
event.value = 50;
write(fd, &event, sizeof(event));
event.type = EV_SYN;
write(fd, &event, sizeof(event));
syslog(LOG_INFO, "Mouse moves 50 in x-coordinate");
sleep(10);
}
close(fd);
return 0;
}
mousemove.service (/usr/lib/systemd/system/mousemove.service) systemctl start mousemove 启动服务进程
[Unit]
Description=mouse auto move
[Service]
EnvironmentFile=/etc/mouse/move.conf
TimeoutStartSec=3600
ExecStart=/usr/bin/mousemove $EVENT
ExecStop=/bin/kill -s QUIT $MAINPID
[Install]
WantedBy=multi-user.target
move.conf
EVENT="/dev/input/event3"
cat /proc/bus/input/devices | grep mouse0|awk '{print $2}'