What you learn: You will learn, how to accept numeric key-press.
Difficulty: 1 of 5
Description:
I wanted my control to accept numeric key press. The fist idea was to compare keyCode with the KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_2 etc constants, but here was a trap - on my desktop keyboard this solution worked ok, but on virtual android keyboard 1 is drawn over Q, etc and when I did a press with a mouse, the keycode did not match to KeyEvent.KEYCODE_1 (it was KeyEvent.KEYCODE_Q I suppose). The solution to workaround this situation is the following:
Using java Syntax Highlighting
- KeyCharacterMap km = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
- char[] numbers = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
- char symbol = km.getMatch(keyCode, numbers);
- if(symbol != '\0') {
- int index = Integer.parseInt(String.valueOf(symbol));
- ...
- }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4

