linux struct input_event结构体详解

来源:互联网 发布:windows软件卸载 编辑:程序博客网 时间:2024/06/16 15:39
查看/dev/input/eventX是什么类型的事件, cat /proc/bus/input/devices

设备有着自己特殊的按键键码,我需要将一些标准的按键,比如0-9,X-Z等模拟成标准按键,比如KEY_0,KEY-Z等,所以需要用到按键模拟,具体 方法就是操作/dev/input/event1文件,向它写入个input_event结构体就可以模拟按键的输入了。

linux/input.h中有定义,这个文件还定义了标准按键的编码等

struct input_event {

struct timeval time; //按键时间

__u16 type; //类型,在下面有定义

__u16 code; //要模拟成什么按键

__s32 value;//是按下还是释放

};

code:

事件的代码.如果事件的类型代码是EV_KEY,该代码code为设备键盘代码.代码植0~127为键盘上的按键代码,0x110~0x116 为鼠标上按键代码,其中0x110(BTN_ LEFT)为鼠标左键,0x111(BTN_RIGHT)为鼠标右键,0x112(BTN_ MIDDLE)为鼠标中键.其它代码含义请参看include/linux/input.h文件. 如果事件的类型代码是EV_REL,code值表示轨迹的类型.如指示鼠标的X轴方向REL_X(代码为0x00),指示鼠标的Y轴方向REL_Y(代码 为0x01),指示鼠标中轮子方向REL_WHEEL(代码为0x08).

type: 

EV_KEY,键盘

EV_REL,相对坐标

EV_ABS,绝对坐标

value:

事件的值.如果事件的类型代码是EV_KEY,当按键按下时值为1,松开时值为0;如果事件的类型代码是EV_ REL,value的正数值和负数值分别代表两个不同方向的值.

/*

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <unistd.h>


#define KEY_Q 16
int main(int argc,char *argv[])
{
    struct input_event s;
    int fd;
    int ret ;
    fd = open("/dev/input/event1",O_RDONLY);
    if(fd<0){
        perror("open");
        return -1;
    }
    while(1){
        ret = 0;
        ret = read(fd,&s,sizeof(s));
        if(ret && s.value!=0){
            printf("<s.type=%d><s.code=%d><s.value=%d>\n",s.type,s.code,s.value);
            if(s.value==1 && s.code==KEY_Q ){
                break;
            }   
        }
    }
    close(fd);
    return 0;
}





原创粉丝点击