[TinyTut] Recognize numeric key press

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut] Recognize numeric key press

Postby benderamp » Fri Nov 30, 2007 6:13 pm

[align=center][TinyTut] - Recognize numeric key press[/align]

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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 KeyCharacterMap km = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
  2.  
  3.                 char[] numbers = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  4.  
  5.                 char symbol = km.getMatch(keyCode, numbers);
  6.  
  7.                 if(symbol != '\0') {
  8.  
  9.                         int index = Integer.parseInt(String.valueOf(symbol));
  10.  
  11.                         ...
  12.  
  13.                 }
  14.  
  15.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
benderamp
Freshman
Freshman
 
Posts: 5
Joined: Sun Nov 25, 2007 5:19 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests