ADB Shell输入事件

adb shell input keyeventadb shell sendevent的基本区别是什么?我应该用哪一个来输入字符?传递给两个命令的键码是否相同?

635801 次浏览

通过adb shell input keyeventevent_codestring将被发送到设备。

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,低级shell点击屏幕上

顺便说一下,如果你试图找到一种方法发送双引号到设备,尝试以下:

Adb shell输入文本“\”

我不确定为什么没有引用的事件代码,但这个解决方法可以工作。此外,如果你正在使用MonkeyDevice(或黑猩猩聊天),你应该在调用MonkeyDevice之前测试每个字符。输入,否则当你尝试发送时,你什么也得不到

另外,如果你想用input命令发送嵌入式空格,请使用%s

adb shell input text 'this%sis%san%sexample'

将产生

这是一个例子

被输入。

%本身不需要转义——只有特殊的%s对被特殊处理。这当然会导致一个明显的问题,即如何输入字面字符串%s,这必须使用两个单独的命令来完成。

更新:

使用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[持续时间(毫秒)]:

adb shell input swipe 100 500 100 1450 100

本例中X1=100, Y1=500, X2=100, Y2=1450, Duration = 100ms

..

LongPress X Y:

adb shell input swipe 100 500 100 500 250

我们使用同样的命令滑动来模拟长按

本例中X=100, Y=500, Duration = 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 < / p >

另一个区别是:

  • "Adb shell输入"调用input.jar来处理和发送来自android框架Java层的键码。
  • "亚行sendevent"实际上是c代码(工具箱实用程序的一部分),它将输入代码直接发送到/dev/input....的Linux输入子系统。

更多详细的代码跟踪到AOSP框架可以在这里找到:

http://www.srcmap.org/sd_share/4/aba57bc6/AOSP_adb_shell_input_Code_Trace.html#RefId=7c8f5285

我为windows用户编写了一个简单的Powershell脚本,将键映射到adb shell输入事件。并通过局域网远程控制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 devices
List of devices attached
3004e25a57192200        device
31002d9e592b7300        device

然后获取您的特定设备id并尝试以下操作

adb -s 31002d9e592b7300 shell input text 'your%stext'

向运行在android设备中的React-Native应用程序发送一个重载调用:adb shell input keyboard text "rr"