Need to intercept HID Keyboard events (and then block them)

后端 未结 3 1765
囚心锁ツ
囚心锁ツ 2020-12-13 10:51

I\'ve got a RFID USB device that registers as a HID device (A USB Keyboard more or less).

I\'m looking for a way to capture this input, and block/filter it before it

相关标签:
3条回答
  • 2020-12-13 11:11

    So I whipped up a proof-of-concept app according to that post I found here

    It does exactly what I require - just though I'd share my solution anyway.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <dirent.h>
    #include <linux/input.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <sys/select.h>
    #include <sys/time.h>
    #include <termios.h>
    #include <signal.h>
    
    int main(int argc, char* argv[])
    {
        struct input_event ev[64];
        int fevdev = -1;
        int result = 0;
        int size = sizeof(struct input_event);
        int rd;
        int value;
        char name[256] = "Unknown";
        char *device = "/dev/input/event3";
    
    
        fevdev = open(device, O_RDONLY);
        if (fevdev == -1) {
            printf("Failed to open event device.\n");
            exit(1);
        }
    
        result = ioctl(fevdev, EVIOCGNAME(sizeof(name)), name);
        printf ("Reading From : %s (%s)\n", device, name);
    
        printf("Getting exclusive access: ");
        result = ioctl(fevdev, EVIOCGRAB, 1);
        printf("%s\n", (result == 0) ? "SUCCESS" : "FAILURE");
    
        while (1)
        {
            if ((rd = read(fevdev, ev, size * 64)) < size) {
                break;
            }
    
            value = ev[0].value;
    
            if (value != ' ' && ev[1].value == 1 && ev[1].type == 1) {
                printf ("Code[%d]\n", (ev[1].code));
            }
        }
    
        printf("Exiting.\n");
        result = ioctl(fevdev, EVIOCGRAB, 1);
        close(fevdev);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-13 11:18

    Ungrabbing requires a "false" value parameter, as below:

    result = ioctl(fevdev, EVIOCGRAB, 0);
    
    0 讨论(0)
  • 2020-12-13 11:20

    You can use EVIOCGRAB ioctl on an event device to capture it exclusively.

    0 讨论(0)
提交回复
热议问题