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

后端 未结 3 1768
囚心锁ツ
囚心锁ツ 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 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    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;
    }
    

提交回复
热议问题