adb模拟三指划动,ADB命令系列之 adb shell input(Android模拟输入)简单总结

最近开发一个新的接口,在调试的时候要手动输入蛮多参数,关键是参数又都太长,就专门看了下 adb 有木有模拟输入,果然adb shell里是有的。这样以后开发调试、自动化测试等就更加方便了。专门简单总结下:

相关文章

重点介绍一些基本的adb命令,例如devices,start-server,kill-server,install,uninstall,push,pull,bugreport,logcat等。

重点介绍一些相对比较复杂的adb命令,主要是adb shell 相关的。例如screencap,monkey,getprop,setprop,pm,am,dumpsys等。

重点介绍adb shell input的用法,包括怎么输入内容,怎么模拟按键,模拟屏幕滑动等各种输入模拟。

重点结合使用场景介绍ADB 的使用,例如解锁手机、截屏、Monkey点击、获取厂商机型等

简介

input可以用来模拟各种输入设备的输入操作。

命令说明

Usage: input [] [...]

The sources are:

trackball

joystick

touchnavigation

mouse

keyboard

gamepad

touchpad

dpad

stylus

touchscreen

The commands and default sources are:

text (Default: touchscreen)

keyevent [--longpress] ... (Default: keyboard)

tap (Default: touchscreen)

swipe [duration(ms)] (Default: touchscreen)

press (Default: trackball)

roll (Default: trackball)

部分参数说明

source对应各种输入源。一般开发中都是用默认值即可。也就是说一般使用中我们的参数中并没有source。

输入命令中text 和 keyevent是通用的;tap和swipe适用于触摸屏;而press和roll用于有触摸球的设备,由于使用的很少,因此不做说明。

模拟输入文本(text)

用法与事例

主要用于在输入框中输入内容。命令很简单。例如:

adb shell input text "hello,world"

注意事项

使用的前提是当前要输入的位置已经获得了焦点。

特殊字符的输入:adb shell input text中空格、’'、&都是有特殊含义的特殊字符,无法直接输入,要想输入只能使用keyevent。

输入过程中左移右移、删除等都需要使用keyevent。

模拟按键(keyevent)

用法与事例

主要用于模拟键盘的输入,因此是在用键盘的地方才用得到。例如:

adb shell input keyevent 67

常用按键:

按键键码

功能

对应Android定义KeyEvent

1

按menu键

KEYCODE_MENU

3

按home键

KEYCODE_HOME

4

按back键

KEYCODE_BACK

21

光标左移

KEYCODE_DPAD_LEFT

22

光标右移

KEYCODE_DPAD_RIGHT

67

按删除按钮

KEYCODE_DEL

完整按键键码查询

模拟屏幕滑动(swipe)

用法与事例

主要用于模拟手指在屏幕的滑动。例如:

adb shell input swipe 0 20 300 500 #意思从屏幕(0,20)滑动到(300,500)

参数含义

四个参数,分别是其实位置的横竖坐标和结束位置的横竖坐标

参数的意思是模拟在屏幕上的直线滑动

参数可以正值,可以负值

模拟屏幕轻触(tap)

用法与事例

主要用于模拟手指在屏幕的轻触点击。例如:

adb shell input tap 100 400

参数含义

两个参数,先横后竖

参数的意思是模拟在屏幕上点击的位置。

### 使用 `adb shell sendevent` 模拟同时划动 为了实现通过命令行工具 `adb shell sendevent` 来模拟多点触控操作,比如滑动,需要了解设备上每一个触摸输入事件对应的 `/dev/input/eventX` 文件路径以及其支持的事件类型和码。通常情况下,不同的手接触会被映射到同一类型的多个连续事件中。 对于滑动的操作来说,这涉及到一系列精心设计的时间戳、按键码(如 ABS_MT_POSITION_X 和 ABS_MT_POSITION_Y)、槽位切换令(ABS_MT_SLOT),还有同步标记来分隔不同阶段的动作[^1]。 下面给出一段 Python 代码作为例子展示如何构建并发送这些序列: ```python import subprocess from time import sleep def execute_adb_command(command): result = subprocess.run(['adb', 'shell'] + command.split(), capture_output=True, text=True) return result.stdout.strip() # 定义个手起始位置(x,y),结束位置(x_end,y_end)及持续时间ms start_positions = [(100, 200), (200, 300), (300, 400)] end_positions = [(50, 150), (150, 250), (250, 350)] duration_ms = 500 for i in range(3): # 对于每根手 x_start, y_start = start_positions[i] x_end, y_end = end_positions[i] commands = [ f'sendevent /dev/input/event7 3 57 {i}', # 设置slot编号 f'sendevent /dev/input/event7 3 48 {x_start}', # 开始横坐标 f'sendevent /dev/input/event7 3 50 {y_start}', # 开始纵坐标 f'sendevent /dev/input/event7 0 2 0' # 同步报告 ] for cmd in commands: execute_adb_command(cmd) sleep(duration_ms/1000.0) for i in range(3): x_end, y_end = end_positions[i] commands = [ f'sendevent /dev/input/event7 3 57 {i}', f'sendevent /dev/input/event7 3 48 {x_end}', f'sendevent /dev/input/event7 3 50 {y_end}', f'sendevent /dev/input/event7 0 2 0' ] for cmd in commands: execute_adb_command(cmd) commands_release = [ f'sendevent /dev/input/event7 3 57 {i}' for i in range(3) ] + ['sendevent /dev/input/event7 0 0 0'] for cmd in commands_release: execute_adb_command(cmd) ``` 这段脚本首先定义了每个手的起点和终点坐标,并设置了整个动作所需的时间长度。接着,在循环体内分别初始化各个手的位置;等待定毫秒数之后更新它们的目标位置;最后释放所有的触摸点以完成一次完整的滑动手势。 需要注意的是,实际应用时可能还需要调整具体的 event 设备文件名 (`/dev/input/event7`) 及其他参数值以便适配特定硬件平台上的差异[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值