直接通过event设备读取input_event键码

//compiler: gcc 4.3.2
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/input.h>
#define DEV_ENENT  "/dev/input/event0"
#define EVENT_STACK_SIZE 16
struct input_event event_stack[EVENT_STACK_SIZE];
int main()
{
 int iFd = -1;
 int iSize = 0;
 int i;
 int retval;
 fd_set rfds;

 iFd = open(DEV_ENENT, O_RDONLY | O_NONBLOCK);
 if(iFd < 0)
 {
  perror(DEV_ENENT);
  return -1;
 }
 while(1)
 {
  
  FD_ZERO(&rfds);
  FD_SET(iFd, &rfds);
  retval = select(iFd + 1, &rfds, NULL, NULL, NULL);
  if (retval == -1)
  {
   perror("select()");
   continue;
  }
  else if (retval)
   printf("Data is available now.\n");
  else
   continue;
  iSize = read(iFd, event_stack, sizeof(event_stack));
  if(iSize >= 0)
  {
   for(i = 0; i < iSize/sizeof(event_stack[0]); i++)
    printf("type = %d\n  \
     code = %d\n  \
     value = %d\n",
     event_stack[i].type,
     event_stack[i].code,
     event_stack[i].value);
  }
  else
  {
   perror(DEV_ENENT);
  }
 }
 close(iFd);
 return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值