KeyUtilspublic class KeyUtils extends Object Reusable methods for generating key events.
Definitions:
Tap refers to pushing and releasing a button (down and up event).
Chord refers to pushing a modifier key, tapping a regular key, and
releasing the modifier key. |
Methods Summary |
---|
public static void | chordMenuKey(android.test.ActivityInstrumentationTestCase test, char shortcutKey)Simulates chording the menu key.
final Instrumentation inst = test.getInstrumentation();
final KeyEvent pushMenuKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
final KeyCharacterMap keyCharMap = KeyCharacterMap.load(pushMenuKey.getDeviceId());
final KeyEvent shortcutKeyEvent = keyCharMap.getEvents(new char[] { shortcutKey })[0];
final int shortcutKeyCode = shortcutKeyEvent.getKeyCode();
inst.sendKeySync(pushMenuKey);
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, shortcutKeyCode));
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, shortcutKeyCode));
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU));
| public static void | longClick(android.test.ActivityInstrumentationTestCase test)Simulates a long click via the keyboard.
final Instrumentation inst = test.getInstrumentation();
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
try {
Thread.sleep((long)(ViewConfiguration.getLongPressTimeout() * 1.5f));
} catch (InterruptedException e) {
e.printStackTrace();
}
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
| public static void | tapMenuKey(android.test.ActivityInstrumentationTestCase test)Simulates tapping the menu key.
final Instrumentation inst = test.getInstrumentation();
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU));
inst.sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU));
|
|