imx6ull开发板,Linux-c编程,识别 键盘、鼠标、触摸屏坐标

复制代码
/* input 调试工具(自动识别 键盘,鼠标的位移和按键,屏幕的单点触摸)*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <string.h>

static const char *ev_type(int type)
{
    switch(type)
    {
        case EV_SYN: return "SYN";  // 同步事件:一批输入事件结束了
        case EV_KEY: return "KEY";  // 按键事件:键盘,鼠标的按键,触摸按下
        case EV_REL: return "REL";  // 相对坐标事件:鼠标
        case EV_ABS: return "ABS";  // 绝对坐标事件:触摸屏,摇杆,多点触摸
        case EV_MSC: return "MSC";  // 杂项事件:原始硬件扫描码,特殊输入设备信息
        default: return "UNKNOWN";
    }
}

static const char *key_state(int v)
{
    if (v == 0) return "up";    // 松开
    if (v == 1) return "down";  // 按下
    if (v == 2) return "REPEAT"; // 长按
    return "?";
}

static void print_device_name(int fd)
{
    char name[256] = "unknown";
    /* ioctl: 从 /dev/input/eventX 获取输入设备名字 input_dev->name, 
        例如: "gpio_keys"  "gt9147" */
    if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) >= 0)
    {
        printf("Device Name: %s\n", name);
    }
}

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s /dev/input/eventX\n", argv[0]);
        return -1;
    }

    int fd = open(argv[1], O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        return -1;
    }

    print_device_name(fd);

    struct input_event ev;

    printf("Start reading input events...\n");

    while (1)
    {
        ssize_t ret = read(fd, &ev, sizeof(ev));
        if (ret < 0)
        {
            perror("read");
            break;
        }
        else if(ret == 0){
            printf("device closed\n");
            break;
        }
        else if(ret != sizeof(ev))
            continue;
/*
        printf("type=%d(%s) code=%d value=%d\n",
            ev.type, ev_type(ev.type), ev.code, ev.value);
*/
        switch (ev.type){
            case  EV_SYN:
                break;
            case EV_KEY: // 键盘,开发板的按键,鼠标的左右按键 中键
                if(ev.code == BTN_LEFT){
                    printf(" => BTN_LEFT code:%d  value:%d\n",ev.code,ev.value);
                }
                else if (ev.code == BTN_RIGHT)
                {
                    printf(" => BTN_RIGHT code:%d  value:%d\n",ev.code,ev.value);
                }
                else if (ev.code == BTN_MIDDLE)
                {
                    printf(" => BTN_MIDDLE code:%d  value:%d\n",ev.code,ev.value);
                }
                else{
                    printf(" => KEY code:%d value:%d %s\n", ev.code,ev.value, key_state(ev.value));
                }
                break;
            case EV_REL:    // 鼠标
                switch(ev.code){
                    case REL_WHEEL: // 滚轮
                        printf(" => REL_WHEEL code:%d value:%d\n", ev.code, ev.value);
                        break;
                    case REL_X: // 鼠标X方向 相对移动
                        printf(" => REL_X code:%d value:%d\n", ev.code, ev.value);
                        break;
                    case REL_Y:
                        printf(" => REL_Y code:%d value:%d\n", ev.code, ev.value);
                        break;
                    default:
                        printf(" => REL code=%d value=%d\n", ev.code, ev.value);
                        break;
                }
                break;
            case EV_ABS: // 单点触摸
                printf(" => ABS code=%d value=%d\n", ev.code, ev.value);
                break;
            default:break;
        }
    }

    close(fd);
    return 0;
}

有线键盘、鼠标插入开发板后,输入下面的命令,查看设备信息:

cat /proc/bus/input/devices

确认 键盘、鼠标、触摸屏 对应的 /dev/input/eventX

键盘:

鼠标:

开发板的gpio按键

触摸屏的单点触摸:

相关推荐
一技安身9 小时前
【信创】银河麒麟V10、统信UOS解压7Z文件
linux·运维·服务器
奔跑的架构师10 小时前
[A-50]ARMv9/v8-DVFS系统架构
linux·arm开发·架构·系统架构·arm
迷途呀10 小时前
CI/CD 自动化部署实践:让代码提交后自动上线
linux·运维·nginx·ci/cd·自动化
赴生-11 小时前
Liunx 操作系统 进程概念(上)
linux
binqian11 小时前
【linux】OpenSSH升级文档
linux·运维·网络
吴声子夜歌11 小时前
Shell编程——if条件语句
linux·运维·shell
Dawn-bit11 小时前
Linux 运维基础扩展:跳板机、堡垒机与物理服务器全流程
linux·运维·服务器·云计算·运维开发
新时代牛马11 小时前
Linux 定时任务:crontab、anacron 与systemd timer
linux·运维·服务器
mounter62512 小时前
Linux 7.2 引入更灵活的块设备错误注入机制
linux·linux kernel·kernel·disk·error injection
cpolar技术支持12 小时前
Docker 容器启动失败怎么查?端口、日志、权限与网络排障完整教程
linux·docker·容器·cpolar·网络排障