基于DragonBoard 410c的input设备应用层编程

       在http://blog.csdn.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








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值