Basic knowledge of

When we analyze the Android keylog, we have to deal with input, so input in the system how to implement? The Input subsystem is carried by default in Android phones, and the default mouse and keyboard events will be generated when the phone is turned on, so that users can touch the screen to click and use the keys. Android keyboard implementation of the system source location \source\frameworks\base\ CMDS \input\ SRC \com\android\commands\input\ input.java

Key code implementation parsing: Java layer code

Input the class definition

public class Input {
    // Defines the debugging information to be printed
    private static final String TAG = "Input";
    private static final String INVALID_ARGUMENTS = "Error: Invalid arguments for command: ";
    // Use map to implement keyword and identifier mapping
    private static final Map<String, Integer> SOURCES = new HashMap<String, Integer>() {{
        put("keyboard", InputDevice.SOURCE_KEYBOARD);
        put("dpad", InputDevice.SOURCE_DPAD);
        put("gamepad", InputDevice.SOURCE_GAMEPAD);
        put("touchscreen", InputDevice.SOURCE_TOUCHSCREEN);
        put("mouse", InputDevice.SOURCE_MOUSE);
        put("stylus", InputDevice.SOURCE_STYLUS);
        put("trackball", InputDevice.SOURCE_TRACKBALL);
        put("touchpad", InputDevice.SOURCE_TOUCHPAD);
        put("touchnavigation", InputDevice.SOURCE_TOUCH_NAVIGATION);
        put("joystick", InputDevice.SOURCE_JOYSTICK);
    }};
Copy the code

SendKeyEvent function definition

// Sends a keyboard event
  private void sendKeyEvent(int inputSource, int keyCode, boolean longpress) {
       // Get the number of milliseconds since startup
       long now = SystemClock.uptimeMillis();
       // Inject keyboard events
        injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 0.0,
                KeyCharacterMap.VIRTUAL_KEYBOARD, 0.0, inputSource));
        if (longpress) {
            injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, 1.0,
                    KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_LONG_PRESS,
                    inputSource));
        }
        injectKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, keyCode, 0.0,
                KeyCharacterMap.VIRTUAL_KEYBOARD, 0.0, inputSource));
    }

Copy the code

SendSwipe function definition

// Function function: realize sliding screen operation
 private void sendSwipe(int inputSource, float x1, float y1, float x2, float y2, int duration) {
        if (duration < 0) {
            duration = 300;
        }
            // Get the number of milliseconds since startup
        long now = SystemClock.uptimeMillis();
        // Inject the touch event
        injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, x1, y1, 1.0 f);
        // Calculate the start time and end time
        long startTime = now;
        long endTime = startTime + duration;
        while (now < endTime) {
            long elapsedTime = now - startTime;
            float alpha = (float) elapsedTime / duration;
            injectMotionEvent(inputSource, MotionEvent.ACTION_MOVE, now, lerp(x1, x2, alpha),
                    lerp(y1, y2, alpha), 1.0 f);
            now = SystemClock.uptimeMillis();
        }
        injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, x2, y2, 0.0 f);
    }

Copy the code

InjectKeyEvent function definition

// Function function: inject event implementation
   private void injectKeyEvent(KeyEvent event) {
       // Displays debugging information
        Log.i(TAG, "injectKeyEvent: " + event);
        // Get the instance event of inputManager
        InputManager.getInstance().injectInputEvent(event,
                InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
    }
Copy the code

InjectMotionEvent function definition

Function function: inject a touch eventprivate void injectMotionEvent(int inputSource, int action, long when, float x, float y, float pressure) {
        final float DEFAULT_SIZE = 1.0 f;
        final int DEFAULT_META_STATE = 0;
        final float DEFAULT_PRECISION_X = 1.0 f;
        final float DEFAULT_PRECISION_Y = 1.0 f;
        final int DEFAULT_DEVICE_ID = 0;
        final int DEFAULT_EDGE_FLAGS = 0;
        MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE,
                DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, DEFAULT_DEVICE_ID,
                DEFAULT_EDGE_FLAGS);
        event.setSource(inputSource);
        Log.i(TAG, "injectMotionEvent: " + event);
        InputManager.getInstance().injectInputEvent(event,
                InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
    }

Copy the code

SendMove function definition

// The function sends a movement event
   private void sendMove(int inputSource, float dx, float dy) {
        // Get the time
        long now = SystemClock.uptimeMillis();
        // Call the injected touch event
        injectMotionEvent(inputSource, MotionEvent.ACTION_MOVE, now, dx, dy, 0.0 f);
    }

Copy the code