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

Android后台服务触发手动事件:OTG键鼠映射手游触控需求问询

Great question! Mapping keyboard inputs to touch events for games like PUBG Mobile is a super practical use case for OTG-connected peripherals. Here's how you can pull this off on Android:

Core Approach

The idea is straightforward: capture keyboard events (either within your own activity or system-wide) and simulate touch gestures that correspond to in-game actions (like reloading, crouching, or shooting).


Method 1: Capture & Simulate Within Your Activity

If you're building a wrapper app or testing within your own activity, you can directly override key event handlers and dispatch touch events locally.

Step 1: Capture Keyboard Input

Override onKeyDown() in your Activity to intercept specific keys:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Intercept keys and map them to touch actions
    switch(keyCode) {
        case KeyEvent.KEYCODE_R:
            // Simulate tap on reload button (adjust coordinates to match your game)
            simulateTap(500, 1000);
            return true; // Consume the event so the system doesn't handle it
        case KeyEvent.KEYCODE_C:
            // Simulate tap on crouch button
            simulateTap(200, 1000);
            return true;
        case KeyEvent.KEYCODE_SPACE:
            // Simulate tap on jump button
            simulateTap(800, 1000);
            return true;
        default:
            return super.onKeyDown(keyCode, event);
    }
}

Step 2: Simulate Touch Events

Create a helper method to generate and dispatch MotionEvent objects for taps:

private void simulateTap(float x, float y) {
    // Create ACTION_DOWN event
    MotionEvent downEvent = MotionEvent.obtain(
        SystemClock.uptimeMillis(),
        SystemClock.uptimeMillis(),
        MotionEvent.ACTION_DOWN,
        x,
        y,
        0
    );
    this.dispatchTouchEvent(downEvent);

    // Create ACTION_UP event (completes the tap)
    MotionEvent upEvent = MotionEvent.obtain(
        SystemClock.uptimeMillis(),
        SystemClock.uptimeMillis(),
        MotionEvent.ACTION_UP,
        x,
        y,
        0
    );
    this.dispatchTouchEvent(upEvent);

    // Recycle events to avoid memory leaks
    downEvent.recycle();
    upEvent.recycle();
}

Method 2: Cross-App Mapping (For Games Like PUBG)

To map keys to touch events in third-party apps (like games), you'll need an Accessibility Service—this lets you monitor system-wide key events and simulate gestures across apps.

Step 1: Register the Accessibility Service

Add this to your AndroidManifest.xml:

<service
    android:name=".KeyboardMappingService"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>
    <meta-data
        android:name="android.accessibilityservice"
        android:resource="@xml/accessibility_config" />
</service>

Step 2: Configure the Service

Create res/xml/accessibility_config.xml to define service permissions:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeKeyEvent|typeWindowStateChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:canPerformGestures="true"
    android:canRequestFilterKeyEvents="true"
    android:description="@string/service_description" />

(Add a description string in strings.xml explaining what the service does—users will see this when enabling it.)

Step 3: Implement the Service Logic

Override onKeyEvent() to handle keys and use dispatchGesture() to simulate taps:

public class KeyboardMappingService extends AccessibilityService {

    @Override
    protected boolean onKeyEvent(KeyEvent event) {
        // Only handle key down events
        if (event.getAction() != KeyEvent.ACTION_DOWN) return super.onKeyEvent(event);

        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_R:
                simulateGameTap(500, 1000); // Reload
                return true;
            case KeyEvent.KEYCODE_C:
                simulateGameTap(200, 1000); // Crouch
                return true;
            case KeyEvent.KEYCODE_F:
                simulateGameTap(600, 800); // Fire
                return true;
        }
        return super.onKeyEvent(event);
    }

    private void simulateGameTap(float x, float y) {
        Path tapPath = new Path();
        tapPath.moveTo(x, y);

        // Build a gesture (tap = short press at the coordinate)
        GestureDescription gesture = new GestureDescription.Builder()
                .addStroke(new GestureDescription.StrokeDescription(
                        tapPath, 0, 100 // Start time, duration in ms
                ))
                .build();

        // Dispatch the gesture to the active app
        dispatchGesture(gesture, null, null);
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // Optional: Detect when PUBG is active to enable/disable mapping
        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            String packageName = event.getPackageName().toString();
            boolean isPubgActive = packageName.equals("com.tencent.ig");
            // Toggle mapping logic based on this flag
        }
    }

    @Override
    public void onInterrupt() {
        // Handle service interruptions (e.g., user disables it)
    }
}

Key Considerations
  • Coordinate Accuracy: Use Android's developer options (enable "Pointer Location") to get exact coordinates of in-game buttons. Note that coordinates vary by screen resolution—you may want to add a UI for users to calibrate positions.
  • Permissions: For the Accessibility Service, users must manually enable it in Settings > Accessibility. Make sure your app guides them through this process.
  • Anti-Cheat Risks: Some games (including PUBG Mobile) have anti-cheat systems that may flag simulated touch events. Test carefully and use this at your own risk.
  • Continuous Actions: For actions like movement (holding W to run), you'll need to simulate a long press (ACTION_DOWN without immediate ACTION_UP) and send ACTION_UP when the key is released.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 11:14:36