按键与KEYCODE使用
一、手机常见按键:
1)HOME 主屏幕键
2) MENU 菜单键
3) BACK 返回键
4) VOLUME_UP 音量加键
5) VOLUME_DOWN 音量减键
6) RecentApps 最近使用app
7) POWER 电源键
8) Dpad 上下左右键
9) ......
二、按键API说明:
返回值 | 方法名 | 描述 |
boolean | pressBace() | 模拟短按返回back键 |
boolean | pressDPadCenter() | 模拟轨迹球中点按键 |
boolean | pressDPadDown() | 模拟轨迹球向下按键 |
boolean | pressDPadLeft() | 模拟轨迹球向左按键 |
boolean | pressDPadRight() | 模拟轨迹球向右按键 |
boolean | pressDPadUp() | 模拟轨迹球向上按键 |
boolean | pressDelete() | 模拟短按删除delete按键 |
boolean | pressEnter() | 模拟短按回车键 |
boolean | pressHome() | 模拟短按home键 |
boolean | pressKeyCode(int keyCode, int metaState) | 模拟短按键盘代码keyCode |
boolean | pressKeyCode(int keyCode) | 模拟短按键盘代码keyCode |
boolean | pressMenu() | 模拟短按menu键 |
boolean | pressRecentApps() | 模拟短按最近使用程序 |
boolean | pressSearch() | 模拟短按搜索键 |
举例:
package com.uiautomatortest; import android.os.Bundle; import android.os.RemoteException; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.testrunner.UiAutomatorTestCase; public class Test extends UiAutomatorTestCase { public void testHome(){ UiDevice.getInstance().pressHome(); sleep(2000); } public void testMenu(){ UiDevice.getInstance().pressMenu(); sleep(2000); } public void testRecent() throws RemoteException{ UiDevice.getInstance().pressRecentApps(); sleep(2000); } }
Test.java
三、KEYCODE 键盘映射码:
1)KeyEvent 按键事件
2)META KEY
辅助功能键:ALT、SHIFT、CAPS_LOCK
列 | 激活状态 | metaState |
base | META_key未被激活 | 0 |
caps | Shift或Caps Lock被激活 | 1 |
fn | Alt被激活 | 2 |
caps_fn | Alt、Shift或Caps Lock同时被激活 | 3 |
举例:
package com.uiautomatortest; import android.os.Bundle; import android.os.RemoteException; import android.view.KeyEvent; import com.android.uiautomator.core.UiDevice; import com.android.uiautomator.testrunner.UiAutomatorTestCase; public class Test extends UiAutomatorTestCase { public void testHome(){ UiDevice.getInstance().pressHome(); sleep(2000); } public void testMenu(){ UiDevice.getInstance().pressMenu(); sleep(2000); } public void testRecent() throws RemoteException{ UiDevice.getInstance().pressRecentApps(); sleep(2000); } public void testKeyCode(){ UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A); //小写a UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B); //小写b UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C); //小写c UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_A,1); //大写A UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_B,1); //大写B UiDevice.getInstance().pressKeyCode(KeyEvent.KEYCODE_C,1); //大写C } }
Test.java
时间: 2024-10-28 15:53:50