基本命令
adb 模拟按键输入的命令主要通过 input 进行
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 26 # 电源键 adb shell input keyevent 82 # 菜单键 adb shell input keyevent 3 # HOME 键 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 # 熄灭屏幕
触击屏幕
adb shell input tap <X> <Y> # x,y为坐标位置
划动屏幕用到了 swipe 命令,它有四个参数,分别是起始点x坐标 起始点y坐标 结束点x坐标 结束点y坐标。
如果锁屏没有密码,是通过滑动手势解锁,那么可以通过 input swipe 来解锁。
# 四个参数:起始点x坐标 起始点y坐标 结束点x坐标 结束点y坐标。 adb shell input swipe 300 1000 300 500 # 向上滑动 adb shell input swipe 300 100 300 1000 # 向下滑动 adb shell input swipe 1000 500 200 500 # 向左滑动 adb shell input swipe 200 500 1000 500 # 向右滑动
在焦点处于某文本框时,可以通过 input 命令来输入文本。
adb shell input text hello # 输入hello
keyevent 命令大全
官方文档地址:https://developer.android.com/reference/android/view/KeyEvent
中文keycode大全:adb——keyevent命令大全