如何通过USB调试快速向Android设备发送实时输入指令并配置按键映射?
Great question! I've tackled similar issues when building PC-to-Android input remapping tools, so let's break down solutions for both your problems:
The 1+ second delay you're seeing is mostly due to the overhead of spawning a new Java-based input command process and launching an adb shell session every time you trigger an action. Here are the most effective fixes:
a. Use adb exec-out instead of adb shell
adb shell spins up a full shell environment for each command, which adds significant overhead. adb exec-out directly executes the command and streams output back without the shell wrapper, cutting down on startup time:
# Instead of this (slow): adb shell input tap 500 500 # Use this (faster): adb exec-out input tap 500 500
b. Replace input with sendevent (native, non-Java)
The input command is a Java wrapper, which is inherently slower. sendevent is a native binary that writes directly to input device nodes, eliminating Java-related latency. First, find your touch input device node:
adb exec-out getevent -l | grep ABS_MT_POSITION_X
Look for output like /dev/input/event2: EV_ABS ABS_MT_POSITION_X 000001f4 — the node path here is /dev/input/event2.
c. Use ADB TCPIP mode (optional, reduces USB overhead)
If you're using USB, switching to TCPIP can reduce some transfer latency:
adb tcpip 5555 adb connect <your-device-ip>:5555
d. Maintain a persistent ADB shell session
Instead of launching a new session for each keypress, keep an interactive shell open and pipe commands to it. For example, in Python, you can use subprocess.Popen to hold the session open and send commands through stdin, avoiding repeated startup costs.
Most recording tools only replay pre-recorded gestures, but you can send real-time touch events directly with sendevent or build a custom script.
Using sendevent for Real-Time Touches
Touch events on Android follow a specific structure: you need to send down, (optional) move, and up events with sync markers. For example, to trigger a touch at the screen center:
- First get your screen resolution:
adb exec-out wm size
If output is Physical size: 1080x2340, center coordinates are 540, 1170.
- Send the touch down event:
adb exec-out sendevent /dev/input/event2 3 53 540 # ABS_MT_POSITION_X = 540 adb exec-out sendevent /dev/input/event2 3 54 1170 # ABS_MT_POSITION_Y = 1170 adb exec-out sendevent /dev/input/event2 3 57 1 # ABS_MT_TRACKING_ID = 1 (unique touch ID) adb exec-out sendevent /dev/input/event2 0 0 0 # Sync event (signals end of batch)
- Send the touch up event:
adb exec-out sendevent /dev/input/event2 3 57 0 # Clear tracking ID adb exec-out sendevent /dev/input/event2 0 0 0 # Sync event
Build a Real-Time Key-to-Touch Script
To map PC keys to Android touches in real-time, use a script that listens for keypress events and sends the corresponding sendevent commands. Here's a Python example using pynput to listen for keyboard input:
from pynput.keyboard import Listener import subprocess # Pre-configure your touch device and screen center TOUCH_DEVICE = "/dev/input/event2" SCREEN_CENTER_X = 540 SCREEN_CENTER_Y = 1170 def send_touch_down(): # Send touch down sequence cmds = [ f"sendevent {TOUCH_DEVICE} 3 53 {SCREEN_CENTER_X}", f"sendevent {TOUCH_DEVICE} 3 54 {SCREEN_CENTER_Y}", f"sendevent {TOUCH_DEVICE} 3 57 1", f"sendevent {TOUCH_DEVICE} 0 0 0" ] for cmd in cmds: subprocess.run(["adb", "exec-out", cmd], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) def send_touch_up(): # Send touch up sequence cmds = [ f"sendevent {TOUCH_DEVICE} 3 57 0", f"sendevent {TOUCH_DEVICE} 0 0 0" ] for cmd in cmds: subprocess.run(["adb", "exec-out", cmd], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) def on_press(key): try: if key.char.lower() == 'k': send_touch_down() except AttributeError: pass def on_release(key): try: if key.char.lower() == 'k': send_touch_up() except AttributeError: pass # Start listening for keyboard events with Listener(on_press=on_press, on_release=on_release) as listener: listener.join()
Install the required dependency first:
pip install pynput
Bonus: Use Scrcpy for Mouse Control + Custom Mapping
For low-latency mouse control, scrcpy is a fantastic open-source tool that already handles real-time mouse input with minimal delay. You can even add custom key mappings to it by creating a configuration file, mapping the K key to a center tap. This avoids reinventing the wheel for mouse control while letting you add your custom key triggers.
内容的提问来源于stack exchange,提问作者asds_asds




