linux下的输入设备编程

来源:互联网 发布:步步高软件下载中心 编辑:程序博客网 时间:2024/06/01 23:40
输入设备包括: 鼠标,键盘,触摸屏,手柄等。在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等图形库里已经实现读取设备文件,处理里面的事件,不需要我们自己处理输入设备事件。

阅读全文
0 0
原创粉丝点击