Android 如何使用 ADB 使用 sendevent 命令将触摸事件发送到设备?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3437686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:11:17  来源:igfitidea点击:

How to use ADB to send touch events to device using sendevent command?

androidautomationadbtouchscreen

提问by Harkish

I am trying to send touch events to a device using AndroidDebugBridge, so that I can do some basic automation for UI tests. I have followed the discussion in LINK. I am able to use sendevent to simulate touch on emulators, but unable to do the same on a device.

我正在尝试使用 AndroidDebugBridge 将触摸事件发送到设备,以便我可以对 UI 测试进行一些基本的自动化。我已经关注了LINK 中的讨论。我可以使用 sendevent 来模拟模拟器上的触摸,但无法在设备上执行相同操作。

Like in above link the emulator seems to send out 6 events for each touch ( xcoord, ycoord, 2 for press,2 for release) and it was easy to use this information to sendevents, but a getevent for the touchscreen for a device seems to generate far too many events.

就像上面的链接一样,模拟器似乎为每次触摸发送了 6 个事件(xcoord,ycoord,2 个用于按下,2 个用于释放)并且很容易使用此信息发送事件,但是设备触摸屏的 getevent 似乎产生太多的事件。

Has somebody managed to send touch from ADB to a device? Could you please share the solution.

有人设法将触摸从 ADB 发送到设备吗?能否请您分享解决方案。

回答by user643154

Android comes with an inputcommand-line tool that can simulate miscellaneous input events. To simulate tapping, it's:

Android 自带一个input命令行工具,可以模拟各种输入事件。要模拟点击,它是:

input tap x y

You can use the adb shell ( > 2.3.5) to run the command remotely:

您可以使用 adb shell (> 2.3.5) 远程运行命令:

adb shell input tap x y

回答by Tomas

In order to do a particular action (for example to open the web browser), you need to first figure out where to tap. To do that, you can first run:

为了执行特定操作(例如打开网络浏览器),您需要首先确定点击的位置。为此,您可以先运行:

adb shell getevent -l

Once you press on the device, at the location that you want, you will see this output:

在设备上按下所需的位置后,您将看到以下输出:

<...>
/dev/input/event3: EV_KEY       BTN_TOUCH            DOWN
/dev/input/event3: EV_ABS       ABS_MT_POSITION_X    000002f5
/dev/input/event3: EV_ABS       ABS_MT_POSITION_Y    0000069e

adb is telling you that a key was pressed (button down) at position 2f5, 69e in hex which is 757 and 1694 in decimal.

adb 告诉您在位置 2f5, 69e(十六进制为 757 和 1694 的十进制)处按下了一个键(按下按钮)。

If you now want to generate the same event, you can use the input tap command at the same position:

如果现在要生成相同的事件,可以在相同位置使用 input tap 命令:

adb shell input tap 757 1694

More info can be found at:

可以在以下位置找到更多信息:

https://source.android.com/devices/input/touch-devices.htmlhttp://source.android.com/devices/input/getevent.html

https://source.android.com/devices/input/touch-devices.html http://source.android.com/devices/input/getevent.html

回答by serv-inc

2.3.5 did not have input tap, just input keyeventand input textYou can use the monkeyrunner for it: (this is a copy of the answer at https://stackoverflow.com/a/18959385/1587329):

2.3.5没有input tap,只是input keyeventinput text您可以使用monkeyrunner它:(这是答案的副本https://stackoverflow.com/a/18959385/1587329):

You might want to use monkeyrunnerlike this:

你可能想像这样使用monkeyrunner

$ monkeyrunner
>>> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
>>> device = MonkeyRunner.waitForConnection()
>>> device.touch(200, 400, MonkeyDevice.DOWN_AND_UP)

You can also do a drag, start activies etc. Have a look at the api for MonkeyDevice.

您还可以进行拖动、启动活动等。查看MonkeyDevice的 api 。

回答by Ekaterina Vlasova

You don't need to use

你不需要使用

adb shell getevent -l

adb shell getevent -l

command, you just need to enable in Developer Options on the device [Show Touch data] to get X and Y.

命令,您只需要在设备上的开发者选项中启用 [显示触摸数据] 即可获取 X 和 Y。

Some more information can be found in my article here: https://mobileqablog.wordpress.com/2016/08/20/android-automatic-touchscreen-taps-adb-shell-input-touchscreen-tap/

更多信息可以在我的文章中找到:https: //mobileqablog.wordpress.com/2016/08/20/android-automatic-touchscreen-taps-adb-shell-input-touchscreen-tap/

回答by Elist

Consider using Android's uiautomator, with adb shell uiautomator [...] or directly using the .jar that comes with the SDK.

考虑使用 Android 的uiautomator、adb shell uiautomator [...] 或直接使用 SDK 附带的 .jar。