C 语言的 UINPUT 设备程序在 Ubuntu 14.04 中不起作用。为什么?第 2 部分:

问题描述 投票:0回答:1

我使用的是Ubuntu 14.04,我正在c中设置虚拟键盘,这需要uinput才能工作。

我的程序应该将“a”键发送到终端,就像我按键盘上的“a”键一样。

这是我的源代码:

int main()
{
int uinp_fd;
int i;

/********** Open uinput file section. **********************/

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

if(uinp_fd < 0)
{

    printf("Unable to open /dev/uinput\n");
    return -1;
}

else printf("Can open /dev/uinput\n");

/********* Setup input device structure section: ***********/

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

snprintf(uinp.name, UINPUT_MAX_NAME_SIZE, "The C Keyboard");
uinp.id.bustype = BUS_USB;  
uinp.id.version = 1;
uinp.id.vendor = 0x1234;
uinp.id.product = 0xfedc;

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


/****** Setup the uinput keyboard device section: **********/

ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
ioctl(uinp_fd, UI_SET_EVBIT, EV_SYN);
ioctl(uinp_fd, UI_SET_EVBIT, EV_REP);

ioctl(uinp_fd, UI_SET_KEYBIT, KEY_A);

if (ioctl(uinp_fd, UI_DEV_CREATE, NULL) < 0)
{
    printf("Unable to create UINPUT device.\n");
    return -1;
}

/*********** Send keypress events to kernel: ***************/

memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);

event.type = EV_KEY;
event.code = KEY_A;
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));

/************** Release keypress 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));

/*** Destroy keyboard device and close the Uinput device: **/

ioctl(uinp_fd, UI_DEV_DESTROY);
close(uinp_fd);

return 0;

}

但是我的程序没有做任何事情。它只是打印出:

Can open /dev/uinput

就是这样...

我做错了什么?谢谢您的帮助!

c ubuntu-14.04 keypress uikeyboard uinput
1个回答
0
投票

我见过很多这样的答案:https://stackoverflow.com/a/40742162/6340338说你应该在执行

sleep(1)
ioctl(uinp_fd, UI_DEV_CREATE)

© www.soinside.com 2019 - 2024. All rights reserved.