700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > input子系统基础之按键2——input设备应用层编程实践

input子系统基础之按键2——input设备应用层编程实践

时间:2021-09-04 01:38:37

相关推荐

input子系统基础之按键2——input设备应用层编程实践

以下内容源于朱有鹏《物联网大讲堂》课程的学习,如有侵权,请告知删除。

一、input设备应用层编程实践1

1、确定设备文件名

(1)应用层操作驱动有2条路:/dev目录下的设备文件,/sys目录下的属性文件

(2)input子系统用的/dev目录下的设备文件,具体一般都是在 /dev/input/eventn

(3)用cat命令来确认某个设备文件名对应哪个具体设备。

实测的键盘是event1,而鼠标是event3。

2、标准接口打开并读取文件

3、解析struct input_event

#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <linux/input.h>#include <string.h>#define DEVICE_KEY"/dev/input/event1"#define DEVICE_MOUSE"/dev/input/event3"int main(void){int fd = -1, ret = -1;struct input_event ev;// 第1步:打开设备文件fd = open(DEVICE_KEY, O_RDONLY);if (fd < 0){perror("open");return -1;}while (1){// 第2步:读取一个event事件包memset(&ev, 0, sizeof(struct input_event));ret = read(fd, &ev, sizeof(struct input_event));if (ret != sizeof(struct input_event)){perror("read");close(fd);return -1;}// 第3步:解析event包,才知道发生了什么样的输入事件printf("%s.\n", (unsigned char *)&ev);}// 第4步:关闭设备close(fd);return 0;}

二、input设备应用层编程实践2

1、解析键盘事件数据、鼠标事件数据

即更换设备文件,第三步换成下面代码,运行查看。

// 第3步:解析event包,才知道发生了什么样的输入事件printf("-------------------------\n");printf("type: %hd\n", ev.type);printf("code: %hd\n", ev.code);printf("value: %d\n", ev.value);printf("\n");

2、事件类型分析(type)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。