Question

I implemented my own custom keyboard for an application that simply maps buttons with text to a key, then sends it to the EditText.

When a button is pressed, I eventually call this method, passing in the EditText to add the character to, and the character to append.

public void keypadPress(EditText etInput, char character) {
    etInput.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, getKeyEvent(character)));
}

getKeyEvent looks like this:

public static int getKeyEvent(char c) {
    switch (c) {
    case '0':
        return KeyEvent.KEYCODE_0;
    case '1':
        return KeyEvent.KEYCODE_1;
    case '2':
        return KeyEvent.KEYCODE_2;
    case '3':
        return KeyEvent.KEYCODE_3;
    case '4':
        return KeyEvent.KEYCODE_4;
    case '5':
        return KeyEvent.KEYCODE_5;
    case '6':
        return KeyEvent.KEYCODE_6;
    case '7':
        return KeyEvent.KEYCODE_7;
    case '8':
        return KeyEvent.KEYCODE_8;
    case '9':
        return KeyEvent.KEYCODE_9;
    case '-':
        return KeyEvent.KEYCODE_MINUS;
    case '.':
        return KeyEvent.KEYCODE_PERIOD;
    case ',':
        return KeyEvent.KEYCODE_COMMA;
    case 'x':
        return KeyEvent.KEYCODE_X;
    default:
        return -1;
    }
}

The EditText type input is set to 'number'. Also, this works completely perfect in Android 4.0+, however whenever I click my 'x' button, it will get mapped to an '8' on API 8 (and maybe below). I've debugged it, and it returns KeyEvent.KEYCODE_X (or 52), but what shows up in the EditText is the number 8. I am completely clueless and would appreciate any form of help.

Also, the first time that I open the screen with this keypad implementation, whenever I click a button for the first time, I get this warning...

10-04 01:16:29.804: W/KeyCharacterMap(8716): Can't open keycharmap file
10-04 01:16:29.804: W/KeyCharacterMap(8716): Error loading keycharmap file '/system/usr/keychars/touchscreen-keypad.kcm.bin'. hw.keyboards.0.devname='touchscreen-keypad'
10-04 01:16:29.804: W/KeyCharacterMap(8716): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

and then after that it's warning free.

Was it helpful?

Solution

Found the Answer:

I also have the digits property set to "0123456789,.-x" which, even though I tried changing inputType to "text", causes it to map KEYCODE_X to '8'. If the editText has either inputType number, or the digits property set, keycodes that aren't numbers will end up incorrect even if you manually dispatch the event. This only happens on API 8 and below as far as I can tell. On Android 4.0+, you can allow letters even though the inputType is "number".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top