Android平台下基于Root权限修改触摸事件坐标(偏移-200)并实现跨应用生效的技术咨询
Alright, let's work through this together—since you've got root access and want to use getevent/sendevent to shift touch inputs globally (even outside your app), here's a solid step-by-step plan that’ll get you there:
The key here is to intercept the original touch events, modify their Y-coordinate, and feed the adjusted events back to the system as a virtual input device. This ensures the shift works everywhere, not just in a single app.
1. Find Your Physical Touch Device
First, you need to identify which input device corresponds to your screen. Run this command:
getevent -l
Look for the device path (like /dev/input/event2) that spits out events when you touch the screen—you’ll see lines with ABS_MT_POSITION_X/ABS_MT_POSITION_Y (multi-touch position data).
To confirm it’s the right device, check its supported events:
getevent -p /dev/input/event2
Make sure it lists ABS_MT_POSITION_Y (usually with code 0035).
2. Set Up the Virtual Input Device (uinput)
To make the modified events system-wide, we’ll create a virtual touch device using the uinput module (most rooted Android devices have this). First, grant permissions to access it:
chmod 666 /dev/uinput
3. Write the Event Forwarding Script
This script will:
- Create a virtual touch device the system recognizes
- Disable the original touch device to avoid duplicate inputs
- Read original touch events, adjust the Y-coordinate, and send them to the virtual device
Create a file named touch_shift.sh with this content:
#!/system/bin/sh # --- CONFIG THESE VALUES FIRST --- ORIGINAL_DEVICE="/dev/input/event2" # Replace with your touch device path VIRTUAL_DEVICE="/dev/uinput" OFFSET=-200 # Shift up by 200 units (negative = up, positive = down) # --- END CONFIG --- # Step 1: Create the virtual touch device # Enable required event types ioctl "$VIRTUAL_DEVICE" UI_SET_EVBIT EV_SYN ioctl "$VIRTUAL_DEVICE" UI_SET_EVBIT EV_KEY ioctl "$VIRTUAL_DEVICE" UI_SET_EVBIT EV_ABS # Enable touch press/release events ioctl "$VIRTUAL_DEVICE" UI_SET_KEYBIT BTN_TOUCH # Enable multi-touch position tracking ioctl "$VIRTUAL_DEVICE" UI_SET_ABSBIT ABS_MT_POSITION_X ioctl "$VIRTUAL_DEVICE" UI_SET_ABSBIT ABS_MT_POSITION_Y ioctl "$VIRTUAL_DEVICE" UI_SET_ABSBIT ABS_MT_TRACKING_ID # Define device metadata (adjust Y range to match your screen resolution!) # Get your screen's Y range from `getevent -p` on the original device (e.g., min 0, max 2339) struct_input_id="$(printf 'x00x00x00x00x00x00x00x00')" # ABS_MT_POSITION_X range: 0 to 1080 (adjust for your screen width) struct_absinfo_x="$(printf 'x00x00x00x00xe8x03x00x00x00x00x00x00x00x00x00x00')" # ABS_MT_POSITION_Y range: 0 to 2339 (adjust for your screen height) struct_absinfo_y="$(printf 'x00x00x00x00x9fx08x00x00x00x00x00x00x00x00x00x00')" # Finalize virtual device creation echo "GlobalTouchShifter" > "$VIRTUAL_DEVICE" echo "1" > "$VIRTUAL_DEVICE" echo "1" > "$VIRTUAL_DEVICE" echo "$struct_input_id" > "$VIRTUAL_DEVICE" echo "0x0003" > "$VIRTUAL_DEVICE" echo "0x30" > "$VIRTUAL_DEVICE" echo "$struct_absinfo_x" > "$VIRTUAL_DEVICE" echo "0x0003" > "$VIRTUAL_DEVICE" echo "0x35" > "$VIRTUAL_DEVICE" echo "$struct_absinfo_y" > "$VIRTUAL_DEVICE" ioctl "$VIRTUAL_DEVICE" UI_DEV_CREATE # Step 2: Disable original touch device to prevent duplicate inputs echo "0" > "/sys/class/input/$(basename "$ORIGINAL_DEVICE")/enable" # Step 3: Read, modify, and forward events getevent "$ORIGINAL_DEVICE" | while read -r line; do # Split event line into components: timestamp, type, code, value PARTS=($line) TYPE=${PARTS[1]} CODE=${PARTS[2]} VALUE=${PARTS[3]} # Adjust Y-coordinate event (ABS_MT_POSITION_Y, code 0035) if [ "$TYPE" = "0003" ] && [ "$CODE" = "0035" ]; then NEW_VALUE=$((VALUE + OFFSET)) # Keep value within screen bounds to avoid weird behavior if [ "$NEW_VALUE" -lt 0 ]; then NEW_VALUE=0 fi # Send modified event to virtual device sendevent "$VIRTUAL_DEVICE" "$TYPE" "$CODE" "$NEW_VALUE" else # Forward all other events as-is sendevent "$VIRTUAL_DEVICE" "$TYPE" "$CODE" "$VALUE" fi done # Cleanup (runs if script is stopped) echo "1" > "/sys/class/input/$(basename "$ORIGINAL_DEVICE")/enable" ioctl "$VIRTUAL_DEVICE" UI_DEV_DESTROY
4. Run the Script
First, give the script execution permissions:
chmod +x touch_shift.sh
Then run it in the background so it keeps working even if you close the terminal:
nohup sh touch_shift.sh &
Important Notes
- Screen Resolution: Update the
struct_absinfo_yline to match your screen’s actual Y-axis range (get this fromgetevent -pon the original device). If you skip this, touch inputs might behave strangely. - Stopping the Script: To revert back to normal touch behavior, kill the script process or run the cleanup commands manually:
echo "1" > "/sys/class/input/event2/enable" # Replace with your device ioctl /dev/uinput UI_DEV_DESTROY - Permissions: Make sure you’re running all commands as root—you might need to prepend
su -if your terminal isn’t already rooted.
内容的提问来源于stack exchange,提问作者Avihu Kochero




