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,摘引部分我覺得有意思的如下:

keycode 含義
3 HOME 鍵
4 返回鍵
5 打開撥號應用
6 掛斷電話
24 增加音量
25 降低音量
26 電源鍵
27 拍照(需要在相機應用里)
64 打開瀏覽器
82 菜單鍵
85 播放/暫停
86 停止播放
87 播放下一首
88 播放上一首
122 移動光標到行首或列表頂部
123 移動光標到行末或列表底部
126 恢復播放
127 暫停播放
164 靜音
176 打開系統設置
187 切換應用
207 打開聯系人
208 打開日歷
209 打開音樂
210 打開計算器
220 降低屏幕亮度
221 提高屏幕亮度
223 系統休眠
224 點亮屏幕
231 打開語音助手
276 如果沒有 wakelock 則讓系統休眠

下面是 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 出現在文本框了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM