捕捉linux中键盘输入对象的键码情况
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <linux/input.h> #include <linux/input-event-codes.h> const char * path = "/dev/input/event4"; int main(void) { int ret; int fd; struct input_event event; fd = open(path,O_RDONLY); if(fd < 0) { perror(path); exit(-1); } while(1) { ret = read(fd,&event,sizeof(struct input_event)); if(ret == sizeof(struct input_event)) { if(event.type != EV_SYN) { //printf("Event: time %ld.%ld,",event.time.tv_sec,event.time.tv_usec); if ( event.type == 1 ) { printf("type:%d,code:%d, state:%s\n", event.type,event.code,(event.value==1? "press" : (event.value==2? "longPress" : "release"))); } } } } close(fd); return 0; }
其中,/dev/input/event4为系统中键盘对应的设备文件, 每个系统可能不一致,因此修改此文件名可以简单捕捉键盘的键码值;
标签:键码,键盘,fd,linux,input,include,type,event From: https://www.cnblogs.com/weijian168/p/18087956