代碼:
//monitor.c
#include <stdio.h> #include <linux/input.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc, char** argv) { int keys_fd; char ret[2]; struct input_event t; keys_fd = open(argv[1], O_RDONLY); if(keys_fd<=0) { printf("error\n"); return -1; } while(1) { read(keys_fd, &t, sizeof(struct input_event)); printf("key %i state %i type %i\n", t.code, t.value, t.type); } close(keys_fd); return 0; }
編譯:gcc -o monitor monitor.c
查看外設:ls -l /dev/input/ # 外設有多個event0到event5,不確定哪個是鼠標哪個是鍵盤,可以都試試
執行:monitor /dev/input/event2