Input命令分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012439416/article/details/61925555

1, 概述

Input主要是和InputManager进行交互,可以模拟键盘的点击事件,鼠标的滑动点击事件等。

平时使用最多的是adb shell input keyevent指令,就以该指令来论述流程,其他类似的指令原理完全一样。

2, keyevent详细流程

  Input的main方法如下,

public static void main(String[] args) {
        (new Input()).run(args);
    } 

直接调用run方法,run中关于keyevent的处理如下,

else if (command.equals("keyevent")) {
                if (length >= 2) {
                    final boolean longpress = "--longpress".equals(args[index + 1]);
                    final int start = longpress ? index + 2 : index + 1;
                    inputSource = getSource(inputSource, InputDevice.SOURCE_KEYBOARD);
                    if (length > start) {
                        for (int i = start; i < length; i++) {
                            int keyCode = KeyEvent.keyCodeFromString(args[i]);
                            if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
                                keyCode = KeyEvent.keyCodeFromString("KEYCODE_" + args[i]);
                            }
                            sendKeyEvent(inputSource, keyCode, longpress);
                        }
                        return;
                    }
                } 

调用sendKeyEvent方法进行处理,

private void sendKeyEvent(int inputSource, int keyCode, boolean longpress) {
        long now = SystemClock.uptimeMillis();
        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));
    }

按键事件包含2部分, KeyEvent.ACTION_DOWN 和KeyEvent.ACTION_UP,由injectKeyEvent方法来完成。该方法如下,

private void injectKeyEvent(KeyEvent event) {
        Log.i(TAG, "injectKeyEvent: " + event);
        InputManager.getInstance().injectInputEvent(event,
                InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
    } 

最后调用InputManager的injectInputEvent方法来完成的。

其实,这些指令最后都是调用injectInputEvent方法来完成的。

展开阅读全文

没有更多推荐了,返回首页