ITPub博客

首页 > 应用开发 > > linux下获取usb键盘按键键值

linux下获取usb键盘按键键值

作者:54xx 时间:2019-09-04 14:46:44 0 删除 编辑

注意一下,USB键盘到底挂载到哪个event下即可(  /dev/input/event0)

比较方便的方法是用: cat /dev/input/eventX   ,没有返回错误就说明有这个设备,按一下按键有回应就是它了

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>

struct input_event buff; 
int fd; 
int read_nu;

int main(int argc, char *argv[])
{
    fd = open("/dev/input/event0", O_RDONLY);
    if (fd < 0)
    { 
        perror("can not open device usbkeyboard!"); 
        return;
    } 
    int i = 0;
    printf("--fd:%d--\n",fd);
    while(1)
    {
        while(read(fd,&buff,sizeof(struct input_event))==0)
        {
            ;
        }
        if(buff.type == 1)
        {
            printf("type:%d code:%d value:%d\n",buff.type,buff.code,buff.value); 
        }
    } 
    
    close(fd); 
    return 1;
}

code:表示的是键值码。

type:0,表示是键盘,1表示是鼠标,和其他

value:1表示按下,0表示没有按下,2表示一直按下

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1976/viewspace-3143405/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    226
  • 访问量
    1611