命令行操作

hexdump -e '16/1 "%02x " "\n"' /dev/input/event0

通过evtest工具测试输入设备

apt install evtest
root@docker:~/# evtest
No device specified, trying to scan all of /dev/input/event*
Available devices:
/dev/input/event0:      Power Button
/dev/input/event1:      AT Translated Set 2 keyboard
/dev/input/event2:      VirtualPS/2 VMware VMMouse
/dev/input/event3:      VirtualPS/2 VMware VMMouse
/dev/input/event4:      QEMU QEMU USB Tablet
Select the device event number [0-4]: 
可用输入设备列表:
/dev/input/event0: 电源按钮(Power Button)
/dev/input/event1: 标准键盘(AT Translated Set 2 keyboard)
/dev/input/event2: 虚拟机鼠标(VirtualPS/2 VMware VMMouse)
/dev/input/event3: 虚拟机鼠标(VirtualPS/2 VMware VMMouse)
/dev/input/event4: 虚拟机平板/触摸设备(QEMU QEMU USB Tablet)
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
#include <sys/ioctl.h>

#define INPUT_DEVICE "/dev/input/event4"

#define SCREEN_WIDTH  1280
#define SCREEN_HEIGHT 800

// 全局保存真实触摸范围
int abs_x_min, abs_x_max;
int abs_y_min, abs_y_max;

int main(void)
{
    int fd;
    struct input_event ev;
    struct input_absinfo absinfo;
    // 打开输入设备
    fd = open(INPUT_DEVICE, O_RDONLY);
    if (fd < 0) {
        perror("open error");
        exit(1);
    }
    // 获取 X 轴范围
    ioctl(fd, EVIOCGABS(ABS_X), &absinfo);
    abs_x_min = absinfo.minimum;
    abs_x_max = absinfo.maximum;

    // 获取 Y 轴范围
    ioctl(fd, EVIOCGABS(ABS_Y), &absinfo);
    abs_y_min = absinfo.minimum;
    abs_y_max = absinfo.maximum;
    
    // 打印获取到的范围
    printf("EVDEV ABS_X range: %d - %d\n", abs_x_min, abs_x_max);
    printf("EVDEV ABS_Y range: %d - %d\n", abs_y_min, abs_y_max);

    while (1)
    {
        // 读取一个输入事件
        read(fd, &ev, sizeof(ev));
        // 1. 绝对坐标 X (触摸屏/鼠标)
        if (ev.type == EV_ABS && ev.code == ABS_X) {
            printf("X: %d\t", (ev.value*SCREEN_WIDTH)/abs_x_max);
        }

        // 2. 绝对坐标 Y
        if (ev.type == EV_ABS && ev.code == ABS_Y) {
            printf("Y: %d\n", (ev.value*SCREEN_HEIGHT)/abs_y_max);
        }
        // 5. 触摸屏按下/抬起
        if (ev.type == EV_KEY && ev.code == BTN_TOUCH) {
            printf("触摸状态: %s\n", ev.value ? "按下" : "抬起");
        }

        // 6. 鼠标左键按下/抬起
        if (ev.type == EV_KEY && ev.code == BTN_LEFT) {
            printf("鼠标左键: %s\n", ev.value ? "按下" : "抬起");
        }
    }
    
    close(fd);
    return 0;
}