zoukankan      html  css  js  c++  java
  • input之按键输入

    通过直接操作驱动来监控键盘,只要程序一旦在后台启动,无论在任何页面都可以监控到按键的数值。

    步骤如下:

    1.找到键盘挂在点:有两种方法

      方法一:在   /dev/input路径下通过  cat even..(1,2,3,4.。)打印操作,并按下键盘看哪个出现的不是回写(乱码状态),就是键盘挂载点,我的是event1;鼠标是event2;

      方法二:在命令行中输入  cat /proc/bus/input/devices 查看设备对应的结点(文件)

    2.打开键盘文件:

      #define      DEV      "/dev/input/event1"

      int keyFd;

      keyFd=open(DEV,RDONLY);

    3.读键盘数据

      问:读的数据应该以什么类型存放?

      解:所有的输入设备都公用一个头文件,即共用这一个框架,头文件为<linux/input.h>,

    /////////////////////////////这是头文件中的结构体,不用写//////////////////////////////////////////////////////////////////////////// 

        struct input_event {
            struct timeval time;
                __u16 type;//按键类型

           —u16code;//按键值
               __s32 value;//按下为1,长按为2,松开为0
            };

    //////////////////////////////////////////////////////////////////////////////////////////////////////

        int ret;      struct input_event    ev;

        ret=read(devFd,&ev,sizeof(ev));

      每一次从键盘读到的值都存在这样的一个结构体中;

    5.访问数据

      通过结构体变量ev可以访问数据。

    代码示例:

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    //设备头文件
    #include <linux/input.h>

    //cat /proc/bus/input/devices 查看设备对应的结点(文件)
    #define KEY_DEV "/dev/input/event3"

    char mapKey(unsigned short code)
    {
    switch(code){
    case KEY_A:return 'a';
    case KEY_B:return 'b';
    case KEY_C:return 'c';
    case KEY_D:return 'd';
    case KEY_E:return 'e';
    case KEY_F:return 'f';
    case KEY_G:return 'g';
    case KEY_5:return '5';
    case KEY_KP4:return '4';
    case KEY_ENTER:return ' ';
    default:return '?';
    }
    }

    int main(void)
    {
    int devFd,ret;
    //input 子系统设备以该结构体提交一个事件
    struct input_event ev;

    devFd=open(KEY_DEV,O_RDONLY);
    if(devFd==-1)
    {
    perror("open Keyboard");
    return 1;
    }

    system("clear");
    system("stty -echo");

    //读按键数据
    while(1)
    {
    ret=read(devFd,&ev,sizeof(ev));
    if(ret==-1)
    {
    perror("read error");
    break;
    }
    switch(ev.type){
    case EV_SYN:;break;
    case EV_KEY:
    if(ev.value==1 || ev.value==2)
    {
    putchar(mapKey(ev.code));
    fflush(stdout);
    }
    break;
    case EV_REL:
    printf("相对 code:%d value:%d ",ev.code,ev.value);break;
    case EV_ABS:
    printf("绝对 code:%d value:%d ",ev.code,ev.value);break;
    }
    }

    system("stty echo");
    close(devFd);
    return 0;
    }

        

  • 相关阅读:
    BeanUtils在web项目中的应用
    BeanUtils的日期问题
    使用BeanUtils组件
    调用数据库过程函数mysql
    sql注入
    如何取SO中的特性
    Read config detail from SO
    Parts-Ufida ERP project 1
    常用医疗英语
    April 24th 2020
  • 原文地址:https://www.cnblogs.com/edan/p/8831186.html
Copyright © 2011-2022 走看看