android uinput 按键_安卓linux中有uinput.h 吗?我使用uinput模拟键盘时,提示linux/uinput.h: No such file or directory...

本文介绍了如何在Linux 2.6.x内核中利用uinput驱动来模拟用户输入,如键盘和鼠标事件。首先加载uinput模块,然后通过打开/dev/uinput设备并配置参数来创建用户输入设备。接着,使用ioctl函数设置事件类型,并通过write函数将键盘事件发送到内核,进而传递给应用程序。示例代码展示了如何发送键盘按键和鼠标点击事件。
摘要由CSDN通过智能技术生成

展开全部

安卓linux中有uinput.h,使用uinput模拟键盘时,提示linux/uinput.h: No such file or directory,可使用如下代码解决:Dashboard January 2007 Issue

Mehul Patel

Using uinput driver in Linux-

2.6.x to send user input

Dashboard January 2007 Issue

Using uinput driver in Linux-2.6.x to send user

input

Introduction:

The Linux 2.6.x provides a “uinput” driver, which helps users to inject data to the Linux kernel.

This is very useful while writing applications to interface customized input devices like wireless

joystick, keyboard etc.

The driver uses /dev/uinput device to send data to kernel space which in turn send data to Xwindows

OR active shell. This feature can be used to perform automated shell scripts which

involve graphical user inputs.

Uinput is configured as a loadable module in most of the Linux kernels. You can load uinput

driver by giving the following commands.

$ modprobe uinput

$ lsmod

The “lsmod” command lists all the drivers loaded in the Linux system. You should see “uinput”

driver in the list.

The next step is to develop a sample application. This application will send the user key

sequence to kernel which is in turn sent to X-Windows or shell.

Opening an input device:

// Open the input device

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

if (uinp_fd == NULL)

{

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

return -1;

}

After opening device you have to configure the uinput device parameters (mouse, keyboard,

etc) using the ioctl() function such as:

ioctl (out_fd, UI_SET_EVBIT, EV_KEY);

ioctl (out_fd, UI_SET_EVBIT, EV_REP);

The EV_KEY and EV_REP inform the uinput driver as the event is a keyboard event and the

key value contains the key repetition property.

Dashboard January 2007 Issue

Sending events to kernel:

All the events coming from the user program will be carried to the kernel space through the

structure "struct input_event" defined in kernels "/usr/include/linux/input.h".

A keyboard can be generated using following piece of code:

event.type = EV_KEY;

event.code = KEY_ENTER;

event.value = 1;

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

The above example will send ENTER key to kernel. This key event in-turn will be sent to user

space application. All the key definitions are defined in “/usr/include/linux/input.h” file.

You can use the following sample code to test Linux uinput interface.

// uinput.c

#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

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 == NULL)

{

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, "PolyVision 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 

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);

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( )

{

// 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 = 100;

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

event.type = EV_REL;

event.code = REL_Y;

event.value = 100;

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));

}

int main()

{

// Return an error if device not found.

if (setup_uinput_device() 

{

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

return -1;

}

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

send_click_events(); // Send mouse event

ioctl(uinp_fd, UI_DEV_DESTROY);

close(uinp_fd);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值