Chinaunix首页 | 论坛 | 博客
  • 博客访问: 332823
  • 博文数量: 97
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 636
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-12 22:41
文章分类

全部博文(97)

文章存档

2017年(8)

2015年(87)

2014年(2)

我的朋友

分类: LINUX

2017-05-31 21:15:02

注意一下,USB键盘到底挂载到哪个event下即可(  /dev/input/event0)
比较方便的方法是用: cat /dev/input/eventX   ,没有返回错误就说明有这个设备,按一下按键有回应就是它了

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <linux/input.h>

  5. struct input_event buff;
  6. int fd;
  7. int read_nu;

  8. int main(int argc, char *argv[])
  9. {
  10.     fd = open("/dev/input/event0", O_RDONLY);
  11.     if (fd < 0)
  12.     {
  13.         perror("can not open device usbkeyboard!");
  14.         return;
  15.     }
  16.     int i = 0;
  17.     printf("--fd:%d--\n",fd);
  18.     while(1)
  19.     {
  20.         while(read(fd,&buff,sizeof(struct input_event))==0)
  21.         {
  22.             ;
  23.         }
  24.         if(buff.type == 1)
  25.         {
  26.             printf("type:%d code:%d value:%d\n",buff.type,buff.code,buff.value);
  27.         }
  28.     }
  29.     
  30.     close(fd);
  31.     return 1;
  32. }

code:表示的是键值码。
type:0,表示是键盘,1表示是鼠标,和其他
value:1表示按下,0表示没有按下,2表示一直按下

阅读(2286) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~