FileDocCategorySizeDatePackage
KeyUtils.javaAPI DocAndroid 5.1 API3379Thu Mar 12 22:22:12 GMT 2015android.util

KeyUtils

public 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.
  • Fields Summary
    Constructors Summary
    Methods Summary
    public static voidchordMenuKey(android.test.ActivityInstrumentationTestCase test, char shortcutKey)
    Simulates chording the menu key.

    param
    test The test case that is being run.
    param
    shortcutKey The shortcut key to tap while 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 voidlongClick(android.test.ActivityInstrumentationTestCase test)
    Simulates a long click via the keyboard.

    param
    test The test case that is being run.

            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 voidtapMenuKey(android.test.ActivityInstrumentationTestCase test)
    Simulates tapping the menu key.

    param
    test The test case that is being run.

            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));