linux 虚拟usb键盘,Linux下虚拟鼠标和键盘

Linux下,首先利用uinput向系统注册一个鼠标,注册成功后,我们就可以通过event向系统发送指令。

Ubunto10.10调试通过,代码如下:

/*!

* \filetestmouse.c

* \brief

* \authordota, makefilesb@163.com

* \version0.00

* \date13-04-19 11:30:16

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

static int uinp_fd = -1;

struct uinput_user_dev uinp; // uInput device structure

struct input_event event; // Input device structure

/* Setup the uinput device */

int setup_uinput_device()

{

//Temporary variable

int i=0;

// // Open the input device

uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);

if (uinp_fd <0)

{

// Dashboard January 2007 Issue

printf("Unable to open /dev/uinput/n");

return -1;

}

memset(&uinp,0,sizeof(uinp)); // Intialize the uInput device to NULL

strncpy(uinp.name, "Touch Screen", UINPUT_MAX_NAME_SIZE);

uinp.id.version = 4;

uinp.id.bustype = BUS_USB;

// // Setup the uinput device

ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);

ioctl(uinp_fd, UI_SET_EVBIT, EV_REL);

ioctl(uinp_fd, UI_SET_RELBIT, REL_X);

ioctl(uinp_fd, UI_SET_RELBIT, REL_Y);

for (i=0; i < 256; i++) {

ioctl(uinp_fd, UI_SET_KEYBIT, i);

}

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_TOUCH);

//ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_FORWARD);

ioctl(uinp_fd, UI_SET_KEYBIT, BTN_BACK);

/* Create input device into input sub-system */

write(uinp_fd, &uinp, sizeof(uinp));

if (ioctl(uinp_fd, UI_DEV_CREATE))

{

printf("Unable to create UINPUT device.");

return -1;

}

return 1;

}

void send_click_events(int x,int y)

{

// // Move pointer to (0,0) location

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_REL;

event.code = REL_X;

//Dashboard January 2007 Issue

event.value = x;

write(uinp_fd, &event, sizeof(event));

event.type = EV_REL;

event.code = REL_Y;

event.value = y;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

// // Report BUTTON CLICK - PRESS event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = BTN_LEFT;

event.value = 1;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

// // Report BUTTON CLICK - RELEASE event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = BTN_LEFT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

}

void send_a_button()

{

// Report BUTTON CLICK - PRESS event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = KEY_A;

//Dashboard January 2007 Issue

event.value = 1;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

// Report BUTTON CLICK - RELEASE event

memset(&event, 0, sizeof(event));

gettimeofday(&event.time, NULL);

event.type = EV_KEY;

event.code = KEY_A;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

event.type = EV_SYN;

event.code = SYN_REPORT;

event.value = 0;

write(uinp_fd, &event, sizeof(event));

}

// /* This function will open the uInput device. Please make

// sure that you have inserted the uinput.ko into kernel. */

int main()

{

// Return an error if device not found.

if (setup_uinput_device() < 0)

{

printf("Unable to find uinput device/n");

return -1;

}

int i;

for( i=0;i<20;i++)

{

send_a_button(); // Send a "A" key

send_click_events(10,10); // Send mouse event

sleep(1);

}

/* Destroy the input device */

ioctl(uinp_fd, UI_DEV_DESTROY);

/* Close the UINPUT device */

close(uinp_fd);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值