深入理解input命令只支持单指,是否可以修改实现多指?-千里马android framework实战开发

背景:

经常使用的input命令如下:
在这里插入图片描述
正常情况下一般要模拟一个触摸点击事件一般用如下命令
input tap 200 300
执行完成后手机画面一般就会响应一个click事件,这里发现一般都是一个手指,一直寻找看看input命令是否可以支持多个手指通过命令同时按下操作的情况,发现并没有这种多指的触摸的命令。难道确实是不支持吗?

input只支持单指源码分析

源码路径
frameworks/base/cmds/input/src/com/android/commands/input/Input.java

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

    private void run(String[] args) {
   
        if (args.length < 1) {
   
            showUsage();
            return;
        }

        int index = 0;
        String command = args[index];
        int inputSource = InputDevice.SOURCE_UNKNOWN;
        if (SOURCES.containsKey(command)) {
   
            inputSource = SOURCES.get(command);
            index++;
            command = args[index];
        }
        final int length = args.length - index;

        try {
   
            if (command.equals("text")) {
   
                if (length == 2) {
   
                    inputSource = getSource(inputSource, InputDevice.SOURCE_KEYBOARD);
                    sendText(inputSource, args[index+1]);
                    return;
                }
            } 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;
                    }
                }
            } else if (command.equals("tap")) {
   //可以看到这里input tap走的是这里
                if (length == 3) {
   
                    inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN);
                    sendTap(inputSource, Float.parseFloat(args[index+1]),
                            Float.parseFloat(args[index+2]));
                    return;
                }
            } else if (command.equals("swipe")) {
   
                int duration = -1;
                inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN);
                switch (length) {
   
                    case 6:
                        duration = Integer.parseInt(args[index+5]);
                    case 5:
                        sendSwipe(inputSource,
                                Float.parseFloat(args[index+1]), Float.parseFloat(args[index+2]),
                                Float.parseFloat(args[index+3]), Float.parseFloat(args[index+4]),
                                duration);
                        return;
                }
            } else if (command.equals("draganddrop")) {
   
                int duration = -1;
                inputSource = getSource(inputSource, InputDevice.SOURCE_TOUCHSCREEN);
                switch (length) {
   
                    case 6:
                        duration = Integer.parseInt(args[index+5]);
                    case 5:
                        sendDragAndDrop(inputSource,
                                Float.parseFloat(args[index+1]), Float.parseFloat(args[index+2]),
                                Float.parseFloat(args[index+3]), Float.parseFloat(args[index+4]),
                                duration);
                        return;
                }
            } else if (command.equals("press")) {
   
                inputSource = getSource(inputSource, InputDevice.SOURCE_TRACKBALL);
                if (length == 1) {
   
                    sendTap(inputSource, 0.0f, 0.0f);
                    return;
                }
            } else if (command.equals("roll")) {
   
                inputSource = getSource(inputSource, InputDevice.SOURCE_TRACKBALL);
                if (length == 3) {
   
                    sendMove(inputSource, Float.parseFloat(args[index+1]),
                            Float.parseFloat(args[index+2]));
                    return;
                }
            } else {
   
                System.err.println("Error: Unknown command: " + command);
                showUsage();
                return;
            }
        } catch (NumberFormatException ex) {
   
        }
        System.err.println(INVALID_ARGUMENTS + command);
        showUsage();
    }

上面代码可以看到最后input tap这个命令最后走入是sendTap方法:

看看这里sendTap

//注意这里传递的参数只有坐标x,y,和一个inputsource,根本没有看到有多个手指的id传入,基本就可以确定这里其实是不支持多指的
   private void sendTap(int inputSource, float x, float y) {
   
        long now = SystemClock.uptimeMillis();
        injectMotionEvent(inputSource, MotionEvent.ACTION_DOWN, now, x, y, 1.0f);
        injectMotionEvent(inputSource, MotionEvent.ACTION_UP, now, x, y, 0.0f);
    }

明显可以看到其实tap就是注入了2个触摸事件,一个是ACTION_DOWN一个是ACTION_UP,按下抬起就是tap也就是常说的click事件。
在看看injectMotionEvent方法:

   private void injectMotionEvent(int inputSource, int action, long when, float x, float y, float pressure) {
   
        final float DEFAULT_SIZE = 1.0f;
        final int DEFAULT_META_STATE = 0;
        final float DEFAULT_PRECISION_X = 1.0f;
        final float DEFAULT_PRECISION_Y = 1.0f;
        final int DEFAULT_EDGE_FLAGS = 0;
        //构建出对应的MotionEvent对象
        MotionEvent event = MotionEvent.obtain(when, when, action, x, y, pressure, DEFAULT_SIZE,
                DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y,
                getInputDeviceId(inputSource), DEFAULT_EDGE_FLAGS);
        event.setSource(inputSource);
        Log.i(TAG, "injectMotionEvent: " + event);
        //然后在通过inputmanager的相关方法注入这个motionevent
        InputManager.getInstance().injectInputEvent(event,
                InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
    }

总结:
input tap命令只是进行传递了x,y坐标值,并没有传递多指相关的任何id,所以是不支持的多指的
在这里插入图片描述

input扩展成多指需求设计

目前只支持单指,那么是否可以考虑让input实现多指呢?
其实是完全可以的,因为MotionEvent构造时候完全可以构造成多个手指的这种MotionEvent,然后进行注入,下面就先进行input命令扩展成支持多指的一个命令设计:

考虑设置一个可以多指点击命令项,暂时命名叫做multiple-tap
所以输入命令如下:

input multiple-tap pointId0 x y pointId1 x y …(省略)
命令解释:
multiple-tap --代表多个手指按下点击情况
pointId1 x y --pointId0代表具体手指索引,可以区分不一样手指,一般0开始,x,y代表是触摸事件的坐标位置

本文章对应视频手把手教你学framework:

七件套专题:在这里插入图片描述
点击这里 https://mp.weixin.qq.com/s/Qv8zjgQ0CkalKmvi8tMGaw

hal+perfetto+surfaceflinger
https://mp.weixin.qq.com/s/LbVLnu1udqExHVKxd74ILg
在这里插入图片描述

私聊作者+v(androidframework007)
视频:https://www.bilibili.com/video/BV1wc41117L4/

相关推荐

  1. 大数据-企业级数据仓库项目实战

    2024-02-23 03:58:01       51 阅读
  2. qml实现左右滑动,上下滑动

    2024-02-23 03:58:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-23 03:58:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-23 03:58:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-23 03:58:01       87 阅读
  4. Python语言-面向对象

    2024-02-23 03:58:01       96 阅读

热门阅读

  1. Leetcode | 231. 2 的幂 C语言

    2024-02-23 03:58:01       59 阅读
  2. QT 如何让多语言翻译变得简单,提高效率?

    2024-02-23 03:58:01       51 阅读
  3. Springcloud OpenFeign 的实现(二)

    2024-02-23 03:58:01       56 阅读
  4. Python笔记-super().init(root)的作用

    2024-02-23 03:58:01       54 阅读
  5. git常用命令记录

    2024-02-23 03:58:01       44 阅读
  6. Unity架构师经验分享:基于状态机的逻辑控制

    2024-02-23 03:58:01       54 阅读
  7. golang GOPATH 和GOROOT

    2024-02-23 03:58:01       54 阅读
  8. 【动态规划题目讲解】洛谷P8392 Uplifting Excursion

    2024-02-23 03:58:01       54 阅读
  9. 函数——递归4(c++)

    2024-02-23 03:58:01       52 阅读
  10. Factory Method

    2024-02-23 03:58:01       52 阅读