ADB 命令之 模拟按键/输入

ADB 命令之 模拟按键/输入

模拟按键/输入

在 ​​adb shell​​​ 里有个很实用的命令叫 ​​input​​,通过它可以做一些有趣的事情。

​​input​​ 命令的完整 help 信息如下:

Usage: input [<source>] <command> [<arg>...]

The sources are:

mouse

keyboard

joystick

touchnavigation

touchpad

trackball

stylus

dpad

gesture

touchscreen

gamepad

The commands and default sources are:

text <string> (Default: touchscreen)

keyevent [--longpress] <key code number or name> ... (Default: keyboard)

tap <x> <y> (Default: touchscreen)

swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)

press (Default: trackball)

roll <dx> <dy> (Default: trackball)

比如使用 ​​adb shell input keyevent <keycode>​​​ 命令,不同的 keycode 能实现不同的功能,完整的 keycode 列表详见 ​ ​KeyEvent​​,摘引部分我觉得有意思的如下:

下面是 ​​input​​ 命令的一些用法举例。

电源键

命令:
adb shell input keyevent 26

执行效果相当于按电源键。

菜单键

命令:

adb shell input keyevent 82

HOME 键

命令:

adb shell input keyevent 3

返回键

命令:

adb shell input keyevent 4

音量控制

增加音量:

adb shell input keyevent 24

降低音量:

adb shell input keyevent 25

静音:

adb shell input keyevent 164

媒体控制

播放/暂停:

adb shell input keyevent 85

停止播放:

adb shell input keyevent 86

播放下一首:

adb shell input keyevent 87

播放上一首:

adb shell input keyevent 88

恢复播放:

adb shell input keyevent 126

暂停播放:

adb shell input keyevent 127

点亮/熄灭屏幕

可以通过上文讲述过的模拟电源键来切换点亮和熄灭屏幕,但如果明确地想要点亮或者熄灭屏幕,那可以使用如下方法。

点亮屏幕:

adb shell input keyevent 224

熄灭屏幕:

adb shell input keyevent 223

滑动解锁

如果锁屏没有密码,是通过滑动手势解锁,那么可以通过 ​​input swipe​​ 来解锁。

命令(参数以机型 Nexus 5,向上滑动手势解锁举例):

adb shell input swipe 300 1000 300 500

参数 ​​300 1000 300 500​​​ 分别表示​​起始点x坐标 起始点y坐标 结束点x坐标 结束点y坐标​​。

输入文本

在焦点处于某文本框时,可以通过 ​​input​​ 命令来输入文本。

命令:
adb shell input text hello

现在 ​​hello​​ 出现在文本框了。

相关推荐
RestCloud12 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud12 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence15 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger21 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud