键盘 Input子系统

应用层测试代码

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <linux/input.h>

#include <sys/fcntl.h>

int main(int argc, char *argv[])

{

       int fd = -1;

       int num;

       size_t rb;

       int version;

       char name[20];

       struct input_event ev;

       int i=0;

       if ((fd = open("/dev/input/event1", O_RDONLY)) < 0)  //打开设备

       {

              perror("open error");

              exit(1);

       }

       while(1)

       {

              rb = read(fd, &ev, sizeof(struct input_event));  //读取设备

              if (rb < (int)sizeof(struct input_event))  //读取错误

              {

                     perror("read error");

                     exit(1);

              }

              if (EV_KEY==ev.type)                     //读取的是否是按键内容

              {

                     if (1 == ev.value)                   //key1被按下

                     printf("key is pressed\n");

                     else                                                       //key1被释放

                     printf("key is released\n");

              }     

       }    

        close(fd);

       return 0;

}

posted on 2016-10-21 11:15  嵌入式操作系统  阅读(231)  评论(0编辑  收藏  举报

导航