簡介
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對(duì)應(yīng)各種輸入源。一般開發(fā)中都是用默認(rèn)值即可。也就是說一般使用中我們的參數(shù)中并沒有source。
輸入命令中text 和 keyevent是通用的;tap和swipe適用于觸摸屏;而press和roll用于有觸摸球的設(shè)備,由于使用的很少,因此不做說明。
模擬輸入文本(text)
用法與事例
主要用于在輸入框中輸入內(nèi)容。命令很簡單。例如:
adb shell input text "hello,world"
注意事項(xiàng)
- 使用的前提是當(dāng)前要輸入的位置已經(jīng)獲得了焦點(diǎn)。
- 特殊字符的輸入:adb shell input text中空格、’'、&都是有特殊含義的特殊字符,無法直接輸入,要想輸入只能使用keyevent。
- 輸入過程中左移右移、刪除等都需要使用keyevent。
模擬按鍵(keyevent)
用法與事例
主要用于模擬鍵盤的輸入,因此是在用鍵盤的地方才用得到。例如:
adb shell input keyevent 67
常用按鍵:
| 按鍵鍵碼 | 功能 | 對(duì)應(yīng)Android定義KeyEvent |
|---|---|---|
| 1 | 按menu鍵 | KEYCODE_MENU |
| 3 | 按home鍵 | KEYCODE_HOME |
| 4 | 按back鍵 | KEYCODE_BACK |
| 21 | 光標(biāo)左移 | KEYCODE_DPAD_LEFT |
| 22 | 光標(biāo)右移 | KEYCODE_DPAD_RIGHT |
| 67 | 按刪除按鈕 | KEYCODE_DEL |
完整按鍵鍵碼查詢
http://developer.android.com/reference/android/view/KeyEvent.html
模擬屏幕滑動(dòng)(swipe)
用法與事例
主要用于模擬手指在屏幕的滑動(dòng)。例如:
adb shell input swipe 0 20 300 500 #意思從屏幕(0,20)滑動(dòng)到(300,500)
參數(shù)含義
- 四個(gè)參數(shù),分別是其實(shí)位置的橫豎坐標(biāo)和結(jié)束位置的橫豎坐標(biāo)
- 參數(shù)的意思是模擬在屏幕上的直線滑動(dòng)
- 參數(shù)可以正值,可以負(fù)值
模擬屏幕輕觸(tap)
用法與事例
主要用于模擬手指在屏幕的輕觸點(diǎn)擊。例如:
adb shell input tap 100 400
參數(shù)含義
- 兩個(gè)參數(shù),先橫后豎
- 參數(shù)的意思是模擬在屏幕上點(diǎn)擊的位置。
adb 截屏并保存圖片到電腦
adb shell screencap /sdcard/a.png // 截屏并保存到手機(jī)上
adb pull /sdcard/a.png // 復(fù)制圖片到電腦