linux下的输入设备编程


输入设备包括: 鼠标,键盘,触摸屏,手柄等。
在linux系统里,如果设备已经驱动好的话(注意手柄在centos7里默认没有支持),可以查看:

cat /proc/bus/input/devices:
输出的信息:

    I: Bus=0019 Vendor=0000 Product=0001 Version=0000
    N: Name="Power Button"
    P: Phys=PNP0C0C/button/input0
    S: Sysfs=/devices/LNXSYSTM:00/device:00/PNP0C0C:00/input/input0
    U: Uniq=
    H: Handlers=kbd event0    //表示设设备对应的驱动产生的设备文件为 "/dev/input/event0"
    B: PROP=0
    B: EV=3                   //表示支持的事件类型
    B: KEY=10000000000000     //表示所支持的按键


    I: Bus=0011 Vendor=0001 Product=0001 Version=ab41
    N: Name="AT Translated Set 2 keyboard"
    P: Phys=isa0060/serio0/input0
    S: Sysfs=/devices/platform/i8042/serio0/input/input3
    U: Uniq=
    H: Handlers=sysrq kbd event3 
    B: PROP=0
    B: EV=120013
    B: KEY=402000000 3803078f800d001 feffffdfffefffff fffffffffffffffe
    B: MSC=10
    B: LED=7


    I: Bus=0011 Vendor=0002 Product=0007 Version=01b1
    N: Name="SynPS/2 Synaptics TouchPad"
    P: Phys=isa0060/serio2/input0
    S: Sysfs=/devices/platform/i8042/serio2/input/input5
    U: Uniq=
    H: Handlers=mouse0 event5 
    B: PROP=9
    B: EV=b
    B: KEY=6420 30000 0 0 0 0
    B: ABS=260800011000003

由此可以看出,我pc上的键盘对应的设备文件为"/dev/input/event3",  触摸板对应的设备文件为"/dev/input/event5"
当我们要编程获取对应的设备数据,操作相应的设备文件即可。
可通过linux内核源码"Documentation/input/input.txt"得知,应用程序从输入设备驱动里获取到的数据是以结构体struct input_event为单位的.

struct input_event {
    struct timeval time;  //用于计算数据提交的间隔时间
    unsigned short type;  
    unsigned short code;
    unsigned int value;
};

   当type的值为EV_KEY时,表示得到按键事件, 这时code的值为键码, value的值表示是键码对应的键是按下(1)或松手(0)
   当type的值为EV_ABS时,表示得到绝对坐标事件, 这时code的值表示此次是关于xy坐标发生变动, value的值表示坐标值type的值为EV_REL时,表示得到相对坐标事件, 这时code的值表示此次是关于xy坐标发生变动, value的正负值表示移动的方向及幅度type的值为EV_SYN时,表示数据更新完毕. code/value的值为0

 注意一个输入设备可能会有多种事件, 如一个触摸屏有绝对坐标(EV_ABS), 还有触摸键事件(EV_KEY), 数据更新事件(EV_SYN)

测试代码:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>


int main(int argc, char *argv[])
{
    int fd, ret;

    if (argc < 2)
    {
        printf("usage: %s  /dev/input/event* \n", argv[0]);
        return 0;
    }

    fd = open(argv[1], O_RDONLY);
    if (fd < 0)
        return 1;

    struct input_event evt;
    while (1)
    {
        ret = read(fd, &evt, sizeof(evt));
        switch (evt.type)
        {       
        case EV_KEY:
            printf("key: %d  %s\n", evt.code, evt.value?"pressed":"released");
            break;
        case EV_ABS:
            if (evt.code == ABS_X)
                printf("touch: x = %d\n", evt.value);
            if (evt.code == ABS_Y)
                printf("touch: y = %d\n", evt.value);
            break;
        case EV_REL:
            if (evt.code == REL_X)
                printf("rel: x = %d\n", evt.value);
            if (evt.code == REL_Y)
                printf("rel: y = %d\n", evt.value);
            break;  
        case EV_SYN:
            printf("sync\n");
            break;
        default:
            printf("%d, %d, %d\n", evt.type, evt.code, evt.value);
        }

    }

    close(fd);
    return 0;
}

另: 在SDL, Qt等图形库里已经实现读取设备文件,处理里面的事件,不需要我们自己处理输入设备事件。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值