struct input_event详解

查看/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的正数值和负数值分别代表两个不同方向的值.

/*

* Event types

*/

#define EV_SYN 0x00

#define EV_KEY 0x01 //按键

#define EV_REL 0x02 //相对坐标(轨迹球)

#define EV_ABS 0x03 //绝对坐标

#define EV_MSC 0x04 //其他

#define EV_SW 0x05

#define EV_LED 0x11 //LED

#define EV_SND 0x12//声音

#define EV_REP 0x14//repeat

#define EV_FF 0x15 

#define EV_PWR 0x16

#define EV_FF_STATUS 0x17

#define EV_MAX 0x1f

#define EV_CNT (EV_MAX+1)

 

原创代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>  
#include <linux/input.h> 


int main(void)  
{  
    struct input_event ev_temp;  
    int fd = open("/dev/input/event0", O_RDWR);  
    if(fd < 0) {  
        printf("open device failed.\n");  
        return 0;  
    }  
    printf("open successfully!\n");  


    int count;


char up[] = "抬起";
char down[] = "按下";
  
    while(1) {  
        count = read(fd, &ev_temp, sizeof(struct input_event));
if(count)
{
if(ev_temp.type == EV_SYN)
continue;

printf("key:%d ", ev_temp.code);
printf("%s\n", ev_temp.value?down:up);
continue;
}
    }  
    return 0;  
}

在Qt中,`struct input_event`是一个用于输入设备的结构体,定义如下: ```c++ struct input_event { struct timeval time; // 事件发生的时间戳 __u16 type; // 事件类型 __u16 code; // 事件码 __s32 value; // 事件值 }; ``` 其中,`time`表示事件发生的时间戳,包括秒数和微秒数;`type`表示事件类型,如键盘事件、鼠标事件等;`code`表示事件码,表示具体的按键或者鼠标动作;`value`表示事件的值,对于按键事件通常是0表示按下,1表示释放,对于鼠标事件通常是表示位移量。 当使用Qt的输入设备相关类(如`QKeyEvent`、`QMouseEvent`等)时,可以通过`struct input_event`的成员变量来获取输入事件的信息。 例如,可以将`struct input_event`的成员变量作为参数传递给设备文件的读取函数,如下面的例子: ```c++ #include <QCoreApplication> #include <QDebug> #include <QFile> #include <linux/input.h> // input_event结构体定义 int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 打开输入设备文件 QFile deviceFile("/dev/input/event0"); if (!deviceFile.open(QIODevice::ReadOnly)) { qWarning() << "Failed to open device file!"; return 1; } // 读取输入事件 struct input_event event; while (deviceFile.read((char *)&event, sizeof(event)) > 0) { qDebug() << "Type:" << event.type << " Code:" << event.code << " Value:" << event.value; } // 关闭输入设备文件 deviceFile.close(); return 0; } ``` 以上代码中,`/dev/input/event0`是一个输入设备文件路径,可以通过`cat /proc/bus/input/devices`命令来查看。然后,使用`QFile`类打开该文件,进而使用`read`函数读取输入事件。读取到的事件可以通过`struct input_event`的成员变量来获取事件的信息。最后,关闭文件。 注意:以上代码仅为示例,实际使用时需要仔细阅读设备文档并按照要求进行操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bingqingsuimeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值