linux - 无法使用 input.h Linux 生成写入设备内核的击键

标签 linux kernel linux-device-driver keystroke

我正在尝试生成击键并使用输入库将它们写入 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_SYNSYN_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/

相关文章:

c - Linux 内核 - printk 到一个文件

c++ - 在类内部分配函数指针

我们可以在一台 PC 上构建 linux 内核并将其安装在另一台 PC 上吗?

winapi - 在用户空间和内核空间之间共享内存

linux - arm-eabi arm-gnueabi 和 gnueabi-hf 编译器之间的区别

linux - 使用 XGetPointerMapping/XSetPointerMapping 交换鼠标按钮

用于隔离的 Linux awk 命令

c - 在哪里使用 volatile ?

linux - 替换进程中的物理页时触发 BUG 断言

linux - 指定音量后轨道停止工作