uinput模拟鼠标键盘事件
参考文献
https://blog.csdn.net/mcgrady_tracy/article/details/28340931
https://blog.csdn.net/zgp2917/article/details/98739558
直接出示代码
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <linux/uinput.h>
#include <linux/input.h>
static struct uinput_user_dev uinput_dev;
static int uinput_fd;
int creat_user_uinput(void);
void report_key(unsigned int type, unsigned int keycode, unsigned int value);
void report_mouse_position(int px, int py);
void report_mouse_move(int dx, int dy);
void report_mouse_key(uint16_t type, uint16_t keycode, int32_t value);
int main(int argc, char *argv[])
{
int ret = 0;
ret = creat_user_uinput();
if(ret < 0){
printf("%s:%d\n", __func__, __LINE__);
return -1;//error process.
}
sleep(4);// help you to 'hexdump -C /dev/input/event[X]' for test.
sleep(1);
report_key(EV_KEY, KEY_A, 1);// Report BUTTON A CLICK - PRESS event
report_key(EV_KEY, KEY_A, 0);// Report BUTTON A CLICK - RELEASE event
sleep(1);
report_key(EV_KEY, KEY_B, 1);// Report BUTTON B CLICK - PRESS event
report_key(EV_KEY, KEY_B, 0);// Report BUTTON B CLICK - RELEASE event
sleep(1);
report_key(EV_KEY, KEY_C, 1);// Report BUTTON C CLICK - PRESS event
report_key(EV_KEY, KEY_C, 0);// Report BUTTON C CLICK - RELEASE event
sleep(1);
report_key(EV_KEY, KEY_D, 1);// Report BUTTON C CLICK - PRESS event
report_key(EV_KEY, KEY_D, 0);// Report BUTTON C CLICK - RELEASE event
sleep(2);
report_mouse_key(EV_KEY, BTN_LEFT, 1);
report_mouse_key(EV_KEY, BTN_LEFT, 0);
sleep(2);
report_mouse_key(EV_KEY, BTN_RIGHT, 1);
report_mouse_key(EV_KEY, BTN_RIGHT, 0);
sleep(2);
report_mouse_key(EV_KEY, BTN_LEFT, 1);
report_mouse_key(EV_KEY, BTN_LEFT, 0);
sleep(2);
int px, py; //mouse abslute position
px = py = 100;
int count_position = 0;
while (1) {
report_mouse_position(px, py);
usleep(10*1000);
px += 1;
py += 1;
count_position += 1;
if (count_position >= 500) {
break;
}
}
// There seems to be a conflict between the relative and absolute coordinates of the mouse,
// so the relative coordinate function is temporarily disabled
// sleep(2);
// printf("-------------> 0009\n");
// int dx, dy; //mouse relative position
// dx = dy = 1;
// int count_move = 0;
// while (1) {
// report_mouse_move(fd, dx, dy);
// usleep(10*1000);
// count_move += 1;
// if (count_move >= 300) {
// break;
// }
// }
sleep(2);
ioctl(uinput_fd, UI_DEV_DESTROY);
close(uinput_fd);
return 0;
}
int creat_user_uinput(void)
{
int i;
int ret = 0;
uinput_fd = open("/dev/uinput", O_RDWR | O_NDELAY);
if(uinput_fd < 0){
printf("%s:%d\n", __func__, __LINE__);
return -1;//error process.
}
//to set uinput dev
memset(&uinput_dev, 0, sizeof(struct uinput_user_dev));
snprintf(uinput_dev.name, UINPUT_MAX_NAME_SIZE, "uinput-custom-dev");
uinput_dev.id.version = 1;
uinput_dev.id.bustype = BUS_VIRTUAL; // uinput_dev.id.bustype = BUS_USB;
uinput_dev.id.vendor = 0x1234;
uinput_dev.id.product = 0xfedc;
// action this device support
ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN);
ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY);
//set keyboard event
for(i = 0; i < 256; i++){
ioctl(uinput_fd, UI_SET_KEYBIT, i);
}
//set mouse event
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_LEFT);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_RIGHT);
ioctl(uinput_fd, UI_SET_KEYBIT, BTN_MIDDLE);
ioctl(uinput_fd, UI_SET_EVBIT, EV_ABS);
ioctl(uinput_fd, UI_SET_ABSBIT, ABS_X);
ioctl(uinput_fd, UI_SET_ABSBIT, ABS_Y);
/* There is a conflict between relative coordinates and
absolute coordinates, so the relative coordinate
function is temporarily not used
*/
// ioctl(fd, UI_SET_EVBIT, EV_REL);
// ioctl(fd, UI_SET_RELBIT, REL_X);
// ioctl(fd, UI_SET_RELBIT, REL_Y);
uinput_dev.absmin[ABS_X] = 0;
uinput_dev.absmax[ABS_X] = 1920;
uinput_dev.absfuzz[ABS_X] = 0;
uinput_dev.absflat[ABS_X] = 0;
uinput_dev.absmin[ABS_Y] = 0;
uinput_dev.absmax[ABS_Y] = 1080;
uinput_dev.absfuzz[ABS_Y] = 0;
uinput_dev.absflat[ABS_Y] = 0;
ret = write(uinput_fd, &uinput_dev, sizeof(struct uinput_user_dev));
if(ret < 0){
printf("%s:%d\n", __func__, __LINE__);
return ret;//error process.
}
ret = ioctl(uinput_fd, UI_DEV_CREATE);
if(ret < 0){
printf("%s:%d\n", __func__, __LINE__);
close(uinput_fd);
return ret;//error process.
}
}
void report_key(unsigned int type, unsigned int keycode, unsigned int value)
{
struct input_event key_ev;
memset(&key_ev, 0, sizeof(struct input_event));
gettimeofday(&key_ev.time, NULL);
key_ev.type = type;
key_ev.code = keycode;
key_ev.value = value;
if (write(uinput_fd, &key_ev, sizeof(struct input_event)) < 0) {
printf("%s:%d\n", __func__, __LINE__);
}
gettimeofday(&key_ev.time, NULL);
key_ev.type = EV_SYN;
key_ev.code = SYN_REPORT;
key_ev.value = 0;//event status sync
if (write(uinput_fd, &key_ev, sizeof(struct input_event)) < 0) {
printf("%s:%d\n", __func__, __LINE__);
}
}
void report_mouse_position(int px, int py)
{
struct input_event mouse_ev;
memset(&mouse_ev, 0, sizeof(struct input_event));
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = EV_ABS;
mouse_ev.code = ABS_X;
mouse_ev.value = px;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("position error\n");
}
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = EV_ABS;
mouse_ev.code = ABS_Y;
mouse_ev.value = py;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("position error\n");
}
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = EV_SYN;
mouse_ev.code = SYN_REPORT;
mouse_ev.value = 0;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("syn position error\n");
}
}
void report_mouse_move(int dx, int dy)
{
struct input_event mouse_ev;
memset(&mouse_ev, 0, sizeof(struct input_event));
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = EV_REL;
mouse_ev.code = REL_X;
mouse_ev.value = dx;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("move error\n");
}
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = EV_REL;
mouse_ev.code = REL_Y;
mouse_ev.value = dy;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("move error\n");
}
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = EV_SYN;
mouse_ev.code = SYN_REPORT;
mouse_ev.value = 0;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("syn move error\n");
}
}
void report_mouse_key(uint16_t type, uint16_t keycode, int32_t value)
{
struct input_event mouse_ev;
memset(&mouse_ev, 0, sizeof(struct input_event));
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = type;
mouse_ev.code = keycode;
mouse_ev.value = value;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("key report error\n");
}
gettimeofday(&mouse_ev.time, NULL);
mouse_ev.type = EV_SYN;
mouse_ev.code = SYN_REPORT;
mouse_ev.value = 0;
if (write(uinput_fd, &mouse_ev, sizeof(struct input_event)) < 0) {
printf("syn key report error\n");
}
}