Answers:
通过adb shell input keyevent
,一个event_code
或一个string
将被发送到设备。
usage: input [text|keyevent]
input text <string>
input keyevent <event_code>
以下event_code
是一些可能的值:
0 --> "KEYCODE_UNKNOWN"
1 --> "KEYCODE_MENU"
2 --> "KEYCODE_SOFT_RIGHT"
3 --> "KEYCODE_HOME"
4 --> "KEYCODE_BACK"
5 --> "KEYCODE_CALL"
6 --> "KEYCODE_ENDCALL"
7 --> "KEYCODE_0"
8 --> "KEYCODE_1"
9 --> "KEYCODE_2"
10 --> "KEYCODE_3"
11 --> "KEYCODE_4"
12 --> "KEYCODE_5"
13 --> "KEYCODE_6"
14 --> "KEYCODE_7"
15 --> "KEYCODE_8"
16 --> "KEYCODE_9"
17 --> "KEYCODE_STAR"
18 --> "KEYCODE_POUND"
19 --> "KEYCODE_DPAD_UP"
20 --> "KEYCODE_DPAD_DOWN"
21 --> "KEYCODE_DPAD_LEFT"
22 --> "KEYCODE_DPAD_RIGHT"
23 --> "KEYCODE_DPAD_CENTER"
24 --> "KEYCODE_VOLUME_UP"
25 --> "KEYCODE_VOLUME_DOWN"
26 --> "KEYCODE_POWER"
27 --> "KEYCODE_CAMERA"
28 --> "KEYCODE_CLEAR"
29 --> "KEYCODE_A"
30 --> "KEYCODE_B"
31 --> "KEYCODE_C"
32 --> "KEYCODE_D"
33 --> "KEYCODE_E"
34 --> "KEYCODE_F"
35 --> "KEYCODE_G"
36 --> "KEYCODE_H"
37 --> "KEYCODE_I"
38 --> "KEYCODE_J"
39 --> "KEYCODE_K"
40 --> "KEYCODE_L"
41 --> "KEYCODE_M"
42 --> "KEYCODE_N"
43 --> "KEYCODE_O"
44 --> "KEYCODE_P"
45 --> "KEYCODE_Q"
46 --> "KEYCODE_R"
47 --> "KEYCODE_S"
48 --> "KEYCODE_T"
49 --> "KEYCODE_U"
50 --> "KEYCODE_V"
51 --> "KEYCODE_W"
52 --> "KEYCODE_X"
53 --> "KEYCODE_Y"
54 --> "KEYCODE_Z"
55 --> "KEYCODE_COMMA"
56 --> "KEYCODE_PERIOD"
57 --> "KEYCODE_ALT_LEFT"
58 --> "KEYCODE_ALT_RIGHT"
59 --> "KEYCODE_SHIFT_LEFT"
60 --> "KEYCODE_SHIFT_RIGHT"
61 --> "KEYCODE_TAB"
62 --> "KEYCODE_SPACE"
63 --> "KEYCODE_SYM"
64 --> "KEYCODE_EXPLORER"
65 --> "KEYCODE_ENVELOPE"
66 --> "KEYCODE_ENTER"
67 --> "KEYCODE_DEL"
68 --> "KEYCODE_GRAVE"
69 --> "KEYCODE_MINUS"
70 --> "KEYCODE_EQUALS"
71 --> "KEYCODE_LEFT_BRACKET"
72 --> "KEYCODE_RIGHT_BRACKET"
73 --> "KEYCODE_BACKSLASH"
74 --> "KEYCODE_SEMICOLON"
75 --> "KEYCODE_APOSTROPHE"
76 --> "KEYCODE_SLASH"
77 --> "KEYCODE_AT"
78 --> "KEYCODE_NUM"
79 --> "KEYCODE_HEADSETHOOK"
80 --> "KEYCODE_FOCUS"
81 --> "KEYCODE_PLUS"
82 --> "KEYCODE_MENU"
83 --> "KEYCODE_NOTIFICATION"
84 --> "KEYCODE_SEARCH"
85 --> "TAG_LAST_KEYCODE"
该sendevent
实用程序发送触摸或键盘事件,以及用于模拟硬件事件的其他事件。有关详细信息,请参阅此文章:Android,低级外壳,在屏幕上单击。
>adb shell input text "a b"
但不起作用。
更新中:
使用adb shell input
:
插入文字:
adb shell input text "insert%syour%stext%shere"
(obs:%s表示空格)
..
事件代码:
adb shell input keyevent 82
(82 ---> MENU_BUTTON)
“有关更多关键事件代码,请参见下面的列表”
..
点按X,Y位置:
adb shell input tap 500 1450
要找到要点击的确切X,Y位置,请转到:
设置>开发人员选项>选中选项POINTER SLOCATION
..
滑动X1 Y1 X2 Y2 [Duration(ms)]:
adb shell input swipe 100 500 100 1450 100
在此示例中X1 = 100,Y1 = 500,X2 = 100,Y2 = 1450,持续时间= 100ms
..
长按XY:
adb shell input swipe 100 500 100 500 250
我们使用相同的命令进行滑动来模拟长按
在这个例子中X = 100,Y = 500,持续时间= 250ms
..
事件代码更新列表:
0 --> "KEYCODE_0"
1 --> "KEYCODE_SOFT_LEFT"
2 --> "KEYCODE_SOFT_RIGHT"
3 --> "KEYCODE_HOME"
4 --> "KEYCODE_BACK"
5 --> "KEYCODE_CALL"
6 --> "KEYCODE_ENDCALL"
7 --> "KEYCODE_0"
8 --> "KEYCODE_1"
9 --> "KEYCODE_2"
10 --> "KEYCODE_3"
11 --> "KEYCODE_4"
12 --> "KEYCODE_5"
13 --> "KEYCODE_6"
14 --> "KEYCODE_7"
15 --> "KEYCODE_8"
16 --> "KEYCODE_9"
17 --> "KEYCODE_STAR"
18 --> "KEYCODE_POUND"
19 --> "KEYCODE_DPAD_UP"
20 --> "KEYCODE_DPAD_DOWN"
21 --> "KEYCODE_DPAD_LEFT"
22 --> "KEYCODE_DPAD_RIGHT"
23 --> "KEYCODE_DPAD_CENTER"
24 --> "KEYCODE_VOLUME_UP"
25 --> "KEYCODE_VOLUME_DOWN"
26 --> "KEYCODE_POWER"
27 --> "KEYCODE_CAMERA"
28 --> "KEYCODE_CLEAR"
29 --> "KEYCODE_A"
30 --> "KEYCODE_B"
31 --> "KEYCODE_C"
32 --> "KEYCODE_D"
33 --> "KEYCODE_E"
34 --> "KEYCODE_F"
35 --> "KEYCODE_G"
36 --> "KEYCODE_H"
37 --> "KEYCODE_I"
38 --> "KEYCODE_J"
39 --> "KEYCODE_K"
40 --> "KEYCODE_L"
41 --> "KEYCODE_M"
42 --> "KEYCODE_N"
43 --> "KEYCODE_O"
44 --> "KEYCODE_P"
45 --> "KEYCODE_Q"
46 --> "KEYCODE_R"
47 --> "KEYCODE_S"
48 --> "KEYCODE_T"
49 --> "KEYCODE_U"
50 --> "KEYCODE_V"
51 --> "KEYCODE_W"
52 --> "KEYCODE_X"
53 --> "KEYCODE_Y"
54 --> "KEYCODE_Z"
55 --> "KEYCODE_COMMA"
56 --> "KEYCODE_PERIOD"
57 --> "KEYCODE_ALT_LEFT"
58 --> "KEYCODE_ALT_RIGHT"
59 --> "KEYCODE_SHIFT_LEFT"
60 --> "KEYCODE_SHIFT_RIGHT"
61 --> "KEYCODE_TAB"
62 --> "KEYCODE_SPACE"
63 --> "KEYCODE_SYM"
64 --> "KEYCODE_EXPLORER"
65 --> "KEYCODE_ENVELOPE"
66 --> "KEYCODE_ENTER"
67 --> "KEYCODE_DEL"
68 --> "KEYCODE_GRAVE"
69 --> "KEYCODE_MINUS"
70 --> "KEYCODE_EQUALS"
71 --> "KEYCODE_LEFT_BRACKET"
72 --> "KEYCODE_RIGHT_BRACKET"
73 --> "KEYCODE_BACKSLASH"
74 --> "KEYCODE_SEMICOLON"
75 --> "KEYCODE_APOSTROPHE"
76 --> "KEYCODE_SLASH"
77 --> "KEYCODE_AT"
78 --> "KEYCODE_NUM"
79 --> "KEYCODE_HEADSETHOOK"
80 --> "KEYCODE_FOCUS"
81 --> "KEYCODE_PLUS"
82 --> "KEYCODE_MENU"
83 --> "KEYCODE_NOTIFICATION"
84 --> "KEYCODE_SEARCH"
85 --> "KEYCODE_MEDIA_PLAY_PAUSE"
86 --> "KEYCODE_MEDIA_STOP"
87 --> "KEYCODE_MEDIA_NEXT"
88 --> "KEYCODE_MEDIA_PREVIOUS"
89 --> "KEYCODE_MEDIA_REWIND"
90 --> "KEYCODE_MEDIA_FAST_FORWARD"
91 --> "KEYCODE_MUTE"
92 --> "KEYCODE_PAGE_UP"
93 --> "KEYCODE_PAGE_DOWN"
94 --> "KEYCODE_PICTSYMBOLS"
...
122 --> "KEYCODE_MOVE_HOME"
123 --> "KEYCODE_MOVE_END"
有关命令的完整列表,请访问:http : //developer.android.com/reference/android/view/KeyEvent.html
adb shell input swipe 200 800 200 400 200
另外,如果要使用输入命令发送嵌入的空格,请使用%s
adb shell input text 'this%sis%san%sexample'
将产生
this is an example
被输入。
%
本身不需要转义-仅对特殊%s
对进行特殊处理。当然,这引出了一个明显的问题,即如何输入文字字符串%s
……您将不得不使用两个单独的命令来完成。
atext "Hey, I'm here"
它就可以了。
顺便说一句,如果您试图找到一种向设备发送双引号的方法,请尝试以下操作:
adb shell输入文本'\“'
我不确定为什么没有引号的事件代码,但是此替代方法可以完成工作。另外,如果您使用的是MonkeyDevice(或ChimpChat),则应在调用MonkeyDevice.type之前先测试每个角色,否则尝试发送“
atext "Hey, I'm here"
它就可以了。
另一个区别:
可以在以下位置找到进入AOSP Framework内部的更多详细代码:
http://www.srcmap.org/sd_share/4/aba57bc6/AOSP_adb_shell_input_Code_Trace.html#RefId=7c8f5285
我为Windows用户编写了一个简单的Powershell脚本,用于将键映射到adb shell输入事件。并通过LAN远程控制Android设备。我不知道是否有人觉得它有用,但是我还是会分享它。
$ip = 192.168.1.8
cd D:\Android\android-sdk-windows\platform-tools\; .\adb.exe disconnect $ip; .\adb.exe connect $ip
$adbKeyNum = @{LeftWindows = "1"; F1 = "3"; Enter = "66"; UpArrow = "19"; DownArrow = "20"; LeftArrow = "21"; RightArrow = "22"; Add = "24";
Subtract = "25"; Backspace = "4"; P = "26"}
while(1 -eq 1){
$keyPress = [Console]::ReadKey($true).Key
if ([string]$keyPress -eq "F10"){
.\adb.exe disconnect $ip
exit
}
elseif ([string]$keyPress -eq "F6"){
$string = Read-Host -Prompt 'Input the string: '
.\adb.exe shell input text $string
}
elseif ($adbKeyNum.ContainsKey([string]$keyPress)){
echo $adbKeyNum.Get_Item([string]$keyPress)
.\adb.exe shell input keyevent $adbKeyNum.Get_Item([string]$keyPress)
}
}
如果要在连接多个设备时向特定设备发送文本。首先使用adb设备查找连接的设备
adb devices
List of devices attached
3004e25a57192200 device
31002d9e592b7300 device
然后获取您的特定设备ID并尝试以下操作
adb -s 31002d9e592b7300 shell input text 'your text'