当前位置:   article > 正文

scrcpy学习--多点触控事件的模拟_scrcpy 模拟点击

scrcpy 模拟点击

 

input tap x y 命令可以进行模拟点击,但是只是一个点的事件,怎么进行多点模拟操作呢?

其实在MotionEvent里面把多点信息写进去就可以了。

 

我们可以参考scrcpy里面的写法,

https://github.com/Genymobile/scrcpy

 

    private boolean injectTouch(int action, long pointerId, Position position, float pressure, int buttons) {

        long now = SystemClock.uptimeMillis();

 

        Point point = device.getPhysicalPoint(position);

        if (point == null) {

            // ignore event

            return false;

        }

 

        int pointerIndex = pointersState.getPointerIndex(pointerId);

        if (pointerIndex == -1) {

            Ln.w("Too many pointers for touch event");

            return false;

        }

// pointersState里对pointer进行添加等管理

        Pointer pointer = pointersState.get(pointerIndex);

        pointer.setPoint(point);

        pointer.setPressure(pressure);

        pointer.setUp(action == MotionEvent.ACTION_UP);

//这里设置pointerProperties, pointerCoords

        int pointerCount = pointersState.update(pointerProperties, pointerCoords);

 

        if (pointerCount == 1) {

            if (action == MotionEvent.ACTION_DOWN) {

                lastTouchDown = now;

            }

        } else {

//设置多点事件的action,如105,105,205

            // secondary pointers must use ACTION_POINTER_* ORed with the pointerIndex

            if (action == MotionEvent.ACTION_UP) {

                action = MotionEvent.ACTION_POINTER_UP | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);

            } else if (action == MotionEvent.ACTION_DOWN) {

                action = MotionEvent.ACTION_POINTER_DOWN | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);

            }

        }

 

        MotionEvent event = MotionEvent

                .obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEVICE_ID_VIRTUAL, 0,

                        InputDevice.SOURCE_TOUCHSCREEN, 0);

        return device.injectEvent(event);

}

 

 

调用injectInputEvent方法

 

    public boolean injectEvent(InputEvent event) {

        return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);

    }

 

    public boolean injectEvent(InputEvent inputEvent, int mode) {

        if (!supportsInputEvents()) {

            throw new AssertionError("Could not inject input event if !supportsInputEvents()");

        }

 

        if (displayId != 0 && !InputManager.setDisplayId(inputEvent, displayId)) {

            return false;

        }

 

        return serviceManager.getInputManager().injectInputEvent(inputEvent, mode);

    }

 

 

设置好几个点的轨迹,组装好MotionEvent, 就可以使用injectInputEvent来注入多点触控事件了。MotionEvent的getActionIndex方法可以获取到当前的操作的事件是哪个点发起的。

 

MotionEvent的log

 

03-14 14:03:41.892 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=357.66882, y[0]=888.5372, toolType[0]=TOOL_TYPE_FINGE

R, id[1]=1, x[1]=866.198, y[1]=1360.2915, toolType[1]=TOOL_TYPE_FINGER, buttonSt

ate=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=0, eve

ntTime=689571391, downTime=689567853, deviceId=8, source=0x1002 }

03-14 14:03:41.909 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=348.67715, y[0]=894.53406, toolType[0]=TOOL_TYPE_FING

ER, id[1]=1, x[1]=866.198, y[1]=1361.7908, toolType[1]=TOOL_TYPE_FINGER, buttonS

tate=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=1, ev

entTime=689571407, downTime=689567853, deviceId=8, source=0x1002 }

03-14 14:03:41.944 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=348.67715, y[0]=894.53406, toolType[0]=TOOL_TYPE_FING

ER, id[1]=1, x[1]=865.1989, y[1]=1362.1178, toolType[1]=TOOL_TYPE_FINGER, button

State=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=1, e

ventTime=689571445, downTime=689567853, deviceId=8, source=0x1002 }

03-14 14:03:41.961 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=348.67715, y[0]=894.53406, toolType[0]=TOOL_TYPE_FING

ER, id[1]=1, x[1]=865.1989, y[1]=1362.79, toolType[1]=TOOL_TYPE_FINGER, buttonSt

ate=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=1, eve

ntTime=689571453, downTime=689567853, deviceId=8, source=0x1002 }

 

连接蓝牙鼠标的点击log

03-14 22:23:51.896 28564 28564 D test614 : MotionEvent { action=ACTION_DOWN, act

ionButton=0, id[0]=0, x[0]=270.19272, y[0]=632.4363, toolType[0]=TOOL_TYPE_MOUSE

, buttonState=BUTTON_PRIMARY, metaState=0, flags=0x0, edgeFlags=0x0, pointerCoun

t=1, historySize=0, eventTime=719579827, downTime=719579827, deviceId=16, source

=0x2002 }

03-14 22:23:52.052 28564 28564 D test614 : MotionEvent { action=ACTION_UP, actio

nButton=0, id[0]=0, x[0]=270.19272, y[0]=632.4363, toolType[0]=TOOL_TYPE_MOUSE,

buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySiz

e=0, eventTime=719579984, downTime=719579827, deviceId=16, source=0x2002 }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/556610
推荐阅读
相关标签
  

闽ICP备14008679号