Example usage for android.view KeyCharacterMap VIRTUAL_KEYBOARD

List of usage examples for android.view KeyCharacterMap VIRTUAL_KEYBOARD

Introduction

In this page you can find the example usage for android.view KeyCharacterMap VIRTUAL_KEYBOARD.

Prototype

int VIRTUAL_KEYBOARD

To view the source code for android.view KeyCharacterMap VIRTUAL_KEYBOARD.

Click Source Link

Document

The id of a generic virtual keyboard with a full layout that can be used to synthesize key events.

Usage

From source file:com.jarklee.materialdatetimepicker.time.TimePickerDialog.java

/**
 * Get the keycode value for AM and PM in the current language.
 *///  w  w  w  . j a  v a2 s . c  o m
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(getLocale()).charAt(i);
            pmChar = mPmText.toLowerCase(getLocale()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[] { amChar, pmChar });
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}