我正在尝试生成击键并使用输入库将它们写入 Linux 内核。我找到了一个示例表格 http://rico-studio.com/linux/read-and-write-to-a-keyboard-device/并做了一个小测试程序。它应该只打印一堆 t 但只有当我自己敲一个键(例如空格)时才会这样做。
#include <linux/input.h>
#include <linux/uinput.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#define EV_PRESSED 1
#define EV_RELEASED 0
#define EV_REPEAT 2
int main() {
int fd = 0;
/*In my case event3 handles the keyboard. This can be checked typing
* cat /proc/bus/input/devices in the terminal
*/
char *device = "/dev/input/event3";
struct input_event event;
memset(&event, 0, sizeof(event));
gettimeofday(&event.time, NULL);
if( (fd = open(device, O_RDWR | O_NONBLOCK )) < 0 )
{
printf("not opened "); // Read or Write to device
return 0;
}
for(int i=0;i <500 ;i++)
{
// usleep(1000);
event.type = EV_KEY;
// Press the key down
event.value = EV_PRESSED;
event.code = KEY_T;
write(fd, &event, sizeof(struct input_event));
// usleep(1000);
// Release the key
event.value = EV_RELEASED;
event.code = KEY_T;
write(fd, &event, sizeof(struct input_event));
usleep(100000);
}
close(fd);
return 0;
}
也许这个击键会刷新内存以及为 t 写入设备内存的生成的击键?所以我想知道我缺少什么让它生成一个击键并将它自己写入内核。
最佳答案
如果您运行 xxd/dev/input3
并分解输出,您可以看到键盘也在发送 EV_SYN
、SYN_REPORT
每个键更改后的事件以标记一组事件的结束。做同样的事情:
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
write(fd, &event, sizeof(struct input_event));
关于linux - 无法使用 input.h Linux 生成写入设备内核的击键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074215/