程式語言 - GNU - C/C++ - Input Event



參考資訊:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/input.h

main.c

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

int main(int argc, char **argv)
{
    int fd = -1;
    struct input_event ev = {0};

    fd = open("/dev/input/event0", O_RDONLY);
    if (fd > 0) {
        while (read(fd, &ev, sizeof(struct input_event))) {
            if (ev.type == EV_KEY) {
                printf("code:%d, value:%d\n", ev.code, ev.value);
            }
        }
        close(fd);
    }
    return 0;
}