You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用evdev模拟触摸输入报错,寻求技术解决方案

Fixing Evdev Touch Input Emulation Errors

Hey there! Let's break down why your touch emulation code is failing and get it working properly. The main issues are incomplete device capabilities and missing required touch-specific events. Here's how to fix it step by step:

Key Issues in Your Original Code

  1. Missing Multi-Touch (MT) Essentials
    Your current capability set only includes ABS_MT_POSITION_X, but touch devices require a full set of MT events (like position Y, tracking ID, and pressure) to be recognized correctly by the system.

  2. Incorrect Key Events
    You've included KEY_A and KEY_B, which are irrelevant for touch input. Instead, touch devices need BTN_TOUCH (to signal a press) and BTN_TOOL_FINGER (to indicate a single-finger touch).

  3. Malformed AbsInfo for MT X Position
    The tuple format (0, 255, 128, 0) for ABS_MT_POSITION_X doesn't match the expected AbsInfo structure (value, min, max, fuzz, flat, resolution). Always use the explicit AbsInfo object for consistency.

Corrected Code with Full Touch Emulation

Here's a complete working example that creates a valid touch device and simulates a press, drag, and release:

from evdev import UInput, AbsInfo, ecodes as e
import time

# Define a valid touch device capability set
capabilities = {
    e.EV_KEY: [e.BTN_TOUCH, e.BTN_TOOL_FINGER],
    e.EV_ABS: [
        # Base X/Y axes (required by some systems)
        (e.ABS_X, AbsInfo(value=0, min=0, max=255, fuzz=0, flat=0, resolution=0)),
        (e.ABS_Y, AbsInfo(0, 0, 255, 0, 0, 0)),
        # Multi-touch mandatory events
        (e.ABS_MT_TRACKING_ID, AbsInfo(0, 0, 65535, 0, 0, 0)),  # Tracks individual touch points
        (e.ABS_MT_POSITION_X, AbsInfo(0, 0, 255, 0, 0, 0)),
        (e.ABS_MT_POSITION_Y, AbsInfo(0, 0, 255, 0, 0, 0)),
        (e.ABS_MT_PRESSURE, AbsInfo(0, 0, 255, 0, 0, 0))  # Optional but recommended for realism
    ]
}

try:
    # Create the virtual touch device
    ui = UInput(capabilities, name='custom-touch-emulator', version=0x1)
    print("Virtual touch device created successfully!")

    # Simulate touch press
    ui.write(e.EV_KEY, e.BTN_TOUCH, 1)
    ui.write(e.EV_KEY, e.BTN_TOOL_FINGER, 1)
    ui.write(e.EV_ABS, e.ABS_MT_TRACKING_ID, 0)  # Assign ID 0 to this touch point
    ui.write(e.EV_ABS, e.ABS_MT_POSITION_X, 50)
    ui.write(e.EV_ABS, e.ABS_MT_POSITION_Y, 50)
    ui.write(e.EV_ABS, e.ABS_X, 50)
    ui.write(e.EV_ABS, e.ABS_Y, 50)
    ui.write(e.EV_ABS, e.ABS_MT_PRESSURE, 200)
    ui.syn()  # Sync events to trigger system recognition
    time.sleep(0.5)

    # Simulate touch drag
    for pos in range(50, 201, 10):
        ui.write(e.EV_ABS, e.ABS_MT_POSITION_X, pos)
        ui.write(e.EV_ABS, e.ABS_MT_POSITION_Y, pos)
        ui.write(e.EV_ABS, e.ABS_X, pos)
        ui.write(e.EV_ABS, e.ABS_Y, pos)
        ui.syn()
        time.sleep(0.1)

    # Simulate touch release
    ui.write(e.EV_KEY, e.BTN_TOUCH, 0)
    ui.write(e.EV_KEY, e.BTN_TOOL_FINGER, 0)
    ui.write(e.EV_ABS, e.ABS_MT_TRACKING_ID, -1)  # Mark touch point as ended
    ui.write(e.EV_ABS, e.ABS_MT_PRESSURE, 0)
    ui.syn()
    time.sleep(0.5)

finally:
    # Clean up the device
    ui.close()
    print("Virtual touch device closed.")

Additional Troubleshooting Tips

  • Verify Input Group Membership: Even after adding yourself to the input group, log out and back in (or restart) to apply the permissions. Check with groups in the terminal to confirm.
  • Load UINPUT Kernel Module: If you still get permission errors, ensure the uinput module is loaded with sudo modprobe uinput. To load it on boot, add uinput to /etc/modules-load.d/uinput.conf.
  • Update Evdev: Make sure you're using the latest version of the library with pip install --upgrade evdev.

内容的提问来源于stack exchange,提问作者ovg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:33:26