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

发布于 2024-09-13 14:35:49 字数 415 浏览 10 评论 0原文

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

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

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

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

滥情哥ㄟ 2024-09-20 14:35:49

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

input tap x y

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

adb shell input tap x y

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

input tap x y

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

adb shell input tap x y
爱她像谁 2024-09-20 14:35:49

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

adb shell getevent -l

一旦您在设备上按下,在您想要的位置,您将看到以下输出:

<...>
/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 告诉您在位置 2f5, 69e(十六进制)处按下了一个键(按钮按下),其中十进制为 757 和 1694。

如果您现在想要生成相同的事件,您可以在同一位置使用输入点击命令:

adb shell input tap 757 1694

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

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

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.

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

adb shell input tap 757 1694

More info can be found at:

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

子栖 2024-09-20 14:35:49

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

您可能想像这样使用monkeyrunner

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

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

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

You might want to use monkeyrunner like this:

$ 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.

安穩 2024-09-20 14:35:49

建立在 Tomas 的答案之上,这是我发现的以整数形式查找位置点击位置的最佳方法:

adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}'

使用 adb shell getevent -l 获取事件列表,使用 grep 获取 ABS_MT_POSITION (获取十六进制触摸事件行),最后使用 awk 获取相关的十六进制值,strip它们为零并将十六进制转换为整数。仅当您按下设备时,才会在终端中连续打印 x 和 y 坐标。

然后您可以使用此 adb shell 命令发送命令:

adb shell input tap x y

Building on top of Tomas's answer, this is the best approach of finding the location tap position as an integer I found:

adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}'

Use adb shell getevent -l to get a list of events, the using grep for ABS_MT_POSITION (gets the line with touch events in hex) and finally use awk to get the relevant hex values, strip them of zeros and convert hex to integer. This continuously prints the x and y coordinates in the terminal only when you press on the device.

You can then use this adb shell command to send the command:

adb shell input tap x y
一花一树开 2024-09-20 14:35:49

你不需要使用

adb shell getevent -l

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

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

You don't need to use

adb shell getevent -l

command, you just need to enable in Developer Options on the device [Show Touch data] to get X and 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/

两相知 2024-09-20 14:35:49

考虑使用 Android 的 uiautomator 以及 adb shell uiautomator [...] 或直接使用SDK自带的.jar即可。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文