android命令行模拟输入事件(文字、按键、触摸等)
前言
通过adb shell input
可以模拟android各种输入事件,比如文字、按键、触摸等等。
adb shell input usage: input [<source>] <command> [<arg>...] the sources are: keyboard mouse joystick touchnavigation touchpad trackball dpad stylus gamepad 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)
基本用法
usage: input [text|keyevent]
input text <string>
input keyevent <event_code>
输入文字
列子:
adb shell input text sample-text
而包含字符可以使用引号包裹,其中%s代表空格。
adb shell input text "insert%syour%stext%shere"
模拟按键
通过adb shell input keyevent
可以模拟按键,后面跟按键code。比如:
adb shell input keyevent 82
所有按键code列表如下:
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"
最新的可以参考官方文档:https://developer.android.com/reference/android/view/keyevent.html
模拟触摸
通过 tap可以模拟触摸事件,参数是<x, y>
adb shell input tap 500 1450
滑动则可以通过swipe模拟,参数是<x1 y1 x2 y2 time>
adb shell input swipe 100 500 100 1450 100
巧妙使用swipe也可以模拟长按事件:
adb shell input swipe 100 500 100 500 250
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
上一篇: 熟悉的气味
下一篇: 湘菜的特点,教你湘菜的简单做法