基于DragonBoard 410c的input设备应用层编程
在http://blog.****.net/weixin_40109283/article/details/78915859博客中,我们已经了解到,当有按键按下时,通过adb命令可以看到有事件上报,如图1所示:
图1
当我们用cat命令去查看/dev/input/event2,然后按下按键,也能看到有事件上报,但看不出上报的信息,如图2所示:
图2
经分析,当有按键按下时,上报的是一个结构体,所以用cat命令无法对该结构体进行解析.该结构体在kernel/include/linux/input.h文件中定义,原型如图3所示:
图3
所以当我们编写应用程序去读取event的值时,应该将读得的数据存放到与之对应的结构体中.
代码如下:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <string.h>
#include <unistd.h>
#define MY_KEY "/dev/input/event2"
int main(void)
{
int fd = -1, ret;
struct input_event ev;
fd = open(MY_KEY, O_RDONLY);
if (fd < 0) {
perror("open file failed\n");
return -1;
}
while(1) {
ret = read(fd, &ev, sizeof(struct input_event));
if (ret != sizeof(struct input_event)) {
perror("read file failed\n");
close(fd);
return -1;
}
printf("=========================\n");
printf("type: %hd\n", ev.type);
printf("code: %hd\n", ev.code);
printf("value: %d\n", ev.value);
printf("\n");
}
close(fd);
return 0;
}
将代码编译生成可执行文件,然后push到开发板的system/bin目录下并运行.当按下按键时就可以看到如图4所示:
图4