简体   繁体   English

用于Ubuntu 14.04的C中的UINPUT设备程序不起作用。 为什么?

[英]UINPUT device program in C for Ubuntu 14.04 does not work. Why?

I am using Ubuntu 14.04, and I am setting up a virtual keyboard in c, which requires uinput to work. 我正在使用Ubuntu 14.04,并且正在c中设置虚拟键盘,这需要uinput才能工作。

I had this root privilege problem: 我有这个root特权问题:

ls -l /dev/uinput 
crw-rw-rw- 1 root root 10, 223 Feb 25 11:11 /dev/uinput

But I solved it by creating a new rule for this. 但是我为此创建了一条新规则来解决它。 It now prints: 现在可以打印:

ls -l /dev/uinput
crw-rw---- 1 root uinput 10, 223 Feb  26 21:11 /dev/uinput

This however still doesn't solve my problem and my program still doesn't work, even with sudo. 但是,这仍然不能解决我的问题,即使使用sudo,我的程序仍然无法正常工作。

Here's my code: 这是我的代码:

int main()
{
int uinp_fd;
int i;

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

ioctl(uinp_fd, UI_DEV_CREATE, NULL);

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

My source code continues on further from there. 我的源代码从此继续。

However, the program always prints "Unable to create UINPUT device.". 但是,程序始终打印“无法创建UINPUT设备”。

sudo command to run my program also doesn't work and the same error message is printed. 用于运行我的程序的sudo命令也无法正常工作,并且会显示相同的错误消息。 What should I do to get the program to work? 我应该怎么做才能使程序正常工作? Thanks. 谢谢。

ioctl(uinp_fd, UI_DEV_CREATE, NULL);

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

You have invoked the UI_DEV_CREATE ioctl twice. 您已两次调用UI_DEV_CREATE ioctl。 The first call will succeed and the second will fail. 第一个电话将成功,第二个电话将失败。 So just delete the first call. 因此,只需删除第一个电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C,循环char *(字符串)数组不起作用。 为什么? - C, looping array of char* (strings) does't work. Why? 如果我在此C程序中输入了大于16383的值,该值将十进制转换为二进制,则它将不起作用。 为什么? - If i enter a value above 16383 in this C program that converts decimal to binary, it doesn't work. Why? C 中的按位运算:无法弄清楚为什么 XOR 不起作用。 我的代码或逻辑有缺陷吗? - Bitwise Operations in C: Can't figure out why XOR does not work. Is my code or logic flawed? 为什么返回在我的程序中不起作用? C - Why does the return not work in my program? C c-memcpy和指针。 仍在工作。 为什么? - c - memcpy and pointers. Still work. Why? 为什么我的 bash 脚本在我的 C 程序中不起作用? - Why does my bash script not work in my C program? 为什么_kbhit()在C程序中只能工作一次? - Why does _kbhit() only work once in a C program? 为什么 Dennis Ritchie 的“C 编程语言”中的这个程序不起作用? - Why does this program in Dennis Ritchie's "The C Programming Language" not work? 为什么在while循环后此C程序不起作用? - Why does this C program not work after the while loop? 为什么这个 C++ 程序对 17 13 3 5 不起作用 - Why does this C++ program doesn't work for 17 13 3 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM