Android Framework 常见解决方案(06)input命令 模拟鼠标滚轮 解决方案

114 篇文章 93 订阅

1 原理

通过input的inject方法对MotionEvent事件进行下发。

2 解决方案(Android O)

在文件 frameworks/base/cmds/input/src/com/android/commands/input/Input.java 中做如下修改:

@@ -145,7 +145,19 @@ public class Input {
                             Float.parseFloat(args[index+2]));
                     return;
                 }
-            } else {
+            } else if (command.equals("scroll")) {
+		System.out.println("scroll command effect");
+		inputSource = getSource(inputSource, InputDevice.SOURCE_MOUSE);
+		System.out.println("length = "+length);
+                if (length == 4) {
+		    sendScroll(inputSource, MotionEvent.AXIS_VSCROLL,
+                            Integer.parseInt(args[index+1]),
+			    Float.parseFloat(args[index+2]),
+                            Float.parseFloat(args[index+3]));
+                    return;
+                }
+            }
+	    else {
                 System.err.println("Error: Unknown command: " + command);
                 showUsage();
                 return;
@@ -156,6 +168,18 @@ public class Input {
         showUsage();
     }
 
+    private void sendScroll(int inputSource,int axis,int axisValue,float x,float y){
+/*
+        MotionEvent.PointerProperties[] pointerProperties = { new MotionEvent.PointerProperties() };
+        MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
+        coords.setAxisValue(axis, axisValue);
+        MotionEvent.PointerCoords[] pointerCoords = { coords };
+	long now = SystemClock.uptimeMillis();
+	injectMotionEventScroll(inputSource, MotionEvent.ACTION_SCROLL, now, 1, pointerProperties, pointerCoords);
+*/
+	injectMotionMouseScroll(inputSource,axis,axisValue,x,y);
+    }
+
     /**
      * Convert the characters of string text into key event's and send to
      * device.*/
@@ -310,6 +334,33 @@ public class Input {
                 InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
     }
 
+    private void injectMotionMouseScroll(int inputSource,int axis, int axisValue, float x,float y) {
+        MotionEvent.PointerProperties[] pointerProperties = { new MotionEvent.PointerProperties() };
+	MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
+	pointerProperties[0].clear();
+	pointerProperties[0].id = 0;
+	pointerProperties[0].toolType = MotionEvent.TOOL_TYPE_MOUSE;
+        coords.setAxisValue(axis, axisValue);
+        MotionEvent.PointerCoords[] pointerCoords = { coords };
+	pointerCoords[0].x = x;
+	pointerCoords[0].y = y;
+        float xPrecision = 1.0f;
+        float yPrecision = 1.0f;
+        int deviceId = 0;
+        int edgeFlags = 0;
+        int flags = 0;
+        System.out.println("InputSource:"+inputSource+" Axis:"+axis+" AxisValue:"+axisValue);
+        MotionEvent event = MotionEvent.obtain(0, System.currentTimeMillis(), MotionEvent.ACTION_SCROLL,
+                1, pointerProperties, pointerCoords, 0, 0, xPrecision, yPrecision, deviceId,
+                edgeFlags, getInputDeviceId(inputSource), flags);
+	event.setSource(inputSource);
+        Log.i(TAG, "injectMotionEvent:"+event);
+	System.out.println("getAxisValue:"+event.getAxisValue(axis));
+	System.out.println("injectMotionEvent: " + event);
+        InputManager.getInstance().injectInputEvent(event,
+                InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
+    }
+
     private static final float lerp(float a, float b, float alpha) {
         return (b - a) * alpha + a;
     }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图王大胜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值