有时PAD或者VR机器上没有物理按键,但却想给应用发送这样的按键来测试应用程序是否OK,可以通过串口或者ADB执行以下命令达到目的:
input keyevent 键值名称
举几个例子:
模拟人的手指触摸HOME按键退到Launcher主界面:input keyevent HOME
模拟Android电视盒的遥控器发送MENU按键:input keyevent MENU
模拟物理键盘按向左键:input keyevent DPAD_LEFT
那后面的键值名称该写什么呢,看这个文件就好了:android/frameworks/base/core/java/android/view/KeyEvent.java,随便截取一段如下: 107 public static final int KEYCODE_1 = 8; 108 /** Key code constant: '2' key. */ 109 public static final int KEYCODE_2 = 9; 110 /** Key code constant: '3' key. */ 111 public static final int KEYCODE_3 = 10; 112 /** Key code constant: '4' key. */ 113 public static final int KEYCODE_4 = 11; 114 /** Key code constant: '5' key. */ 115 public static final int KEYCODE_5 = 12; 116 /** Key code constant: '6' key. */ 117 public static final int KEYCODE_6 = 13; 118 /** Key code constant: '7' key. */ 119 public static final int KEYCODE_7 = 14; 120 /** Key code constant: '8' key. */ 121 public static final int KEYCODE_8 = 15; 122 /** Key code constant: '9' key. */ 123 public static final int KEYCODE_9 = 16; 124 /** Key code constant: '*' key. */ 125 public static final int KEYCODE_STAR = 17; 126 /** Key code constant: '#' key. */ 127 public static final int KEYCODE_POUND = 18; 128 /** Key code constant: Directional Pad Up key. 129 * May also be synthesized from trackball motions. */ 130 public static final int KEYCODE_DPAD_UP = 19; 131 /** Key code constant: Directional Pad Down key. 132 * May also be synthesized from trackball motions. */ 133 public static final int KEYCODE_DPAD_DOWN = 20; 134 /** Key code constant: Directional Pad Left key. 135 * May also be synthesized from trackball motions. */ 136 public static final int KEYCODE_DPAD_LEFT = 21; 137 /** Key code constant: Directional Pad Right key. 138 * May also be synthesized from trackball motions. */ 139 public static final int KEYCODE_DPAD_RIGHT = 22; 140 /** Key code constant: Directional Pad Center key. 141 * May also be synthesized from trackball motions. */ 142 public static final int KEYCODE_DPAD_CENTER = 23; 143 /** Key code constant: Volume Up key. 144 * Adjusts the speaker volume up. */ 145 public static final int KEYCODE_VOLUME_UP = 24; 146 /** Key code constant: Volume Down key. 147 * Adjusts the speaker volume down. */ 148 public static final int KEYCODE_VOLUME_DOWN = 25; 149 /** Key code constant: Power key. */ 150 public static final int KEYCODE_POWER = 26;