簡介
input可以用來模擬各種輸入設(shè)備的輸入操作容劳。
命令說明
Usage: input [<source>] <command> [<arg>...]
The sources are:
trackball
joystick
touchnavigation
mouse
keyboard
gamepad
touchpad
dpad
stylus
touchscreen
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)
部分參數(shù)說明
- source對應(yīng)各種輸入源瓜富。一般開發(fā)中都是用默認值即可。也就是說一般使用中我們的參數(shù)中并沒有source罢荡。
輸入命令中text 和 keyevent是通用的训措;tap和swipe適用于觸摸屏莉测;而press和roll用于有觸摸球的設(shè)備,由于使用的很少阁苞,因此不做說明鞠绰。
模擬輸入文本(text)
用法與事例
主要用于在輸入框中輸入內(nèi)容腰埂。命令很簡單。例如:
adb shell input text "hello,world"
注意事項
- 使用的前提是當前要輸入的位置已經(jīng)獲得了焦點蜈膨。
- 特殊字符的輸入:adb shell input text中空格屿笼、’'、&都是有特殊含義的特殊字符翁巍,無法直接輸入驴一,要想輸入只能使用keyevent。
- 輸入過程中左移右移灶壶、刪除等都需要使用keyevent肝断。
模擬按鍵(keyevent)
用法與事例
主要用于模擬鍵盤的輸入,因此是在用鍵盤的地方才用得到驰凛。例如:
adb shell input keyevent 67
常用按鍵:
按鍵鍵碼 | 功能 | 對應(yīng)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 |
完整按鍵鍵碼查詢
http://developer.android.com/reference/android/view/KeyEvent.html
模擬屏幕滑動(swipe)
用法與事例
主要用于模擬手指在屏幕的滑動胸懈。例如:
adb shell input swipe 0 20 300 500 #意思從屏幕(0,20)滑動到(300,500)
參數(shù)含義
- 四個參數(shù),分別是其實位置的橫豎坐標和結(jié)束位置的橫豎坐標
- 參數(shù)的意思是模擬在屏幕上的直線滑動
- 參數(shù)可以正值恰响,可以負值
模擬屏幕輕觸(tap)
用法與事例
主要用于模擬手指在屏幕的輕觸點擊趣钱。例如:
adb shell input tap 100 400
參數(shù)含義
- 兩個參數(shù),先橫后豎
- 參數(shù)的意思是模擬在屏幕上點擊的位置胚宦。
adb 截屏并保存圖片到電腦
adb shell screencap /sdcard/a.png // 截屏并保存到手機上
adb pull /sdcard/a.png // 復(fù)制圖片到電腦