struct input_event {struct timeval time ;__u16 type ;__u16 code ;__s32 value ;};
3 个成员变量 type、code、value 更为重要
- /*
- * Event types
- */
- #define EV_SYN 0x00 //同步类事件,用于同步事件
- #define EV_KEY 0x01 //按键类事件
- #define EV_REL 0x02 //相对位移类事件(譬如鼠标)
- #define EV_ABS 0x03 //绝对位移类事件(譬如触摸屏)
- #define EV_MSC 0x04 //其它杂类事件
- #define EV_SW 0x05
- #define EV_LED 0x11
- #define EV_SND 0x12
- #define EV_REP 0x14
- #define EV_FF 0x15
- #define EV_PWR 0x16
- #define EV_FF_STATUS 0x17
- #define EV_MAX 0x1f
- #define EV_CNT (EV_MAX+1)
- #define KEY_RESERVED 0
- #define KEY_ESC 1 //ESC 键
- #define KEY_1 2 //数字 1 键
- #define KEY_2 3 //数字 2 键
- #define KEY_TAB 15 //TAB 键
- #define KEY_Q 16 //字母 Q 键
- #define KEY_W 17 //字母 W 键
- #define KEY_E 18 //字母 E 键
- #define KEY_R 19 //字母 R 键
- #define REL_X 0x00 //X 轴
- #define REL_Y 0x01 //Y 轴
- #define REL_Z 0x02 //Z 轴
- #define REL_RX 0x03
- #define REL_RY 0x04
- #define REL_RZ 0x05
- #define REL_HWHEEL 0x06
- #define REL_DIAL 0x07
- #define REL_WHEEL 0x08
- #define REL_MISC 0x09
- #define REL_MAX 0x0f
- #define REL_CNT (REL_MAX+1)
- /*
- * Synchronization events.
- */
- #define SYN_REPORT 0
- #define SYN_CONFIG 1
- #define SYN_MT_REPORT 2
- #define SYN_DROPPED 3
- #define SYN_MAX 0xf
- #define SYN_CNT (SYN_MAX+1)
- 所以的输入设备都需要上报同步事件,上报的同步事件通常是 SYN_REPORT,而 value 值通常为 0。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- int main(int argc, char *argv[])
- {
- struct input_event in_ev = {0};
- int fd = -1;
-
- /* 校验传参 */
- if (2 != argc) {
- fprintf(stderr, "usage: %s
\n" , argv[0]); - exit(-1);
- }
-
- /* 打开文件 */
- if (0 > (fd = open(argv[1], O_RDONLY))) {
- perror("open error");
- exit(-1);
- }
- for ( ; ; ) {
-
- /* 循环读取数据 */
- if (sizeof(struct input_event) !=
- read(fd, &in_ev, sizeof(struct input_event))) {
- perror("read error");
- exit(-1);
- }
- printf("type:%d code:%d value:%d\n",
- in_ev.type, in_ev.code, in_ev.value);
- }
- }
使用开发板按键进行输入设备程序测试
第一行中 type 等于 1 ,表示上报的是按键事件 EV_KEY , code=114 ,打开 input-event-codes.h 头文件进行查找,可以发现 cpde=114 对应的是键盘上的 KEY_VOLUMEDOWN 按键,这个是 ALPHA/Mini 开发板出厂系统已经配置好的。而 value=1 表示按键按下,所以整个第一行的意思就是按键 KEY_VOLUMEDOWN 被按下。第二行,表示上报了 EV_SYN 同步类事件( type=0 )中的 SYN_REPORT 事件( code=0 ),表示本轮数据已经完整、报告同步。第三行,type 等于 1 ,表示按键类事件, code 等于 114 、 value 等于 0 ,所以表示按键 KEY_VOLUMEDOWN 被松开。第四行,又上报了同步事件。可以看到上报按键事件时,对应的 value 等于 2 ,表示长按状态
按键应用编程(同时可以做触摸屏点触判断,单点和多点)
# 以字母 A 键为例KEY_A //上报 KEY_A 事件SYN_REPORT //同步
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- int main(int argc, char *argv[])
- {
- struct input_event in_ev = {0};
- int fd = -1;
- int value = -1;
-
- /* 校验传参 */
- if (2 != argc) {
- fprintf(stderr, "usage: %s
\n" , argv[0]); - exit(-1);
- }
- /* 打开文件 */
- if (0 > (fd = open(argv[1], O_RDONLY))) {
- perror("open error");
- exit(-1);
- }
- for ( ; ; ) {
-
- /* 循环读取数据 */
- if (sizeof(struct input_event) !=
- read(fd, &in_ev, sizeof(struct input_event))) {
- perror("read error");
- exit(-1);
- }
- if (EV_KEY == in_ev.type) { //按键事件
-
- switch (in_ev.value) {
- case 0:
- printf("code<%d>: 松开\n", in_ev.code);
- break;
- case 1:
- printf("code<%d>: 按下\n", in_ev.code);
- break;
- case 2:
- printf("code<%d>: 长按\n", in_ev.code);
- break;
- }
- }
- }
- }
./testApp /dev/input/event2 # 测试开发板上的 KEY0