gpt4 book ai didi

c - 在 linux 下使用 ioctl 重新映射键盘

转载 作者:IT王子 更新时间:2023-10-29 01:26:28 24 4
gpt4 key购买 nike

我实际上正在尝试编写一个小程序来捕获来自 linux 下特定 USB 键盘的全局键盘输入。

我正在测试这段代码:

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <string.h>
#include <stdio.h>

static const char *const evval[3] = {
"RELEASED",
"PRESSED ",
"REPEATED"
};

int main(void)
{
const char *dev = "/dev/input/event2";
struct input_event ev;
ssize_t n;
int fd;
char name[256]= "Unknown";

// int codes[2];

// codes[0] = 58; /* M keycap */
// codes[1] = 49; /* assign to N */

fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
return EXIT_FAILURE;
}

if(ioctl(fd, EVIOCGNAME(sizeof(name)), name) > 0)
{
printf("The device on %s says its name is '%s'\n", dev, name);
}

/*
int err = ioctl(fd, EVIOCSKEYCODE, codes);
if (err) {
perror("evdev ioctl");
}*/

while (1) {
n = read(fd, &ev, sizeof ev);
if (n == (ssize_t)-1) {
if (errno == EINTR)
continue;
else
break;
} else
if (n != sizeof ev) {
errno = EIO;
break;
}

if (ev.type == EV_KEY && ev.value >= 0 && ev.value <= 2)
printf("%s 0x%04x (%d)\n", evval[ev.value], (int)ev.code, (int)ev.code);

}

fflush(stdout);
fprintf(stderr, "%s.\n", strerror(errno));

return EXIT_FAILURE;
}

重点是我不知道如何通过其他方式更改某些输入键。我尝试通过更改事件代码在当前红色事件上调用 write(),发送的 key 仍然是前一个,并且我尝试将 ioctl 与 EVIOCSKEYCODE 一起使用,但调用失败并出现“无效参数”错误(我不是一定要正确调用它)。

如何正确更改输出的 key ?

最佳答案

使用 EVIOCGRAB ioctl 获取输入设备,以便通过读取事件来使用它们。通常(未抓取)事件在您阅读时不会被消耗。 ioctl 有一个额外的参数,(int)1 用于抓取,(int)0 用于释放。

要重新注入(inject)任何事件,只需将它们写入 uinput 设备。见例如。一个mouse example here . (事件结构是同类型的,你只需要先给uinput设备写一个struct uinput_user_dev结构,来描述你的新输入设备(提供映射的事件).)

换句话说,您不重新映射:您消费并转发。

关于c - 在 linux 下使用 ioctl 重新映射键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com