need tutorial for key mapping

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

need tutorial for key mapping

Postby ekambresh » Thu Feb 14, 2008 1:05 pm

hi all, i am new to android can any one send me the source code which will guide me how to map keys in android
ekambresh
ekambresh
ekambresh
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 08, 2008 8:54 am
Location: Bangalore

Top

Postby res » Thu Feb 14, 2008 7:38 pm

onKeyDown is thrown whenever a key press occurs.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public boolean onKeyDown(int keyCode, KeyEvent event)
  3.  
  4. {
  5.  
  6.     if (keyCode == KeyEvent.KEYCODE_A)
  7.  
  8.     {
  9.  
  10.         your code here when 'A' is pressed
  11.  
  12.     }
  13.  
  14.     else if (keyCode == KeyEvent.KEYCODE_B)
  15.  
  16.     {
  17.  
  18.          your code here when 'B' is pressed
  19.  
  20.     }
  21.  
  22.                                
  23.  
  24.     return true;
  25.  
  26. }
  27.  
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


edit: fixed code typo ;)
Last edited by res on Fri Feb 15, 2008 4:25 pm, edited 1 time in total.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby plusminus » Fri Feb 15, 2008 11:51 am

Hello res,

you had a typo "return return" in your post. ;)
:!: Remember this:
Returns
* If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

I mixed that up and wonder once or twice :roll: why nothing happened ^^

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: need tutorial for key mapping

Postby mmin18 » Fri Feb 15, 2008 3:59 pm

ekambresh wrote:hi all, i am new to android can any one send me the source code which will guide me how to map keys in android
ekambresh


the mobile phone's keyboard is different from the PC keyboard (101 standard), so it's quite different when you process the onKeyDown/onKeyUp. (like when you push ALT+A, your expected is '4' but the keycode is 'a', and the event.getMetaState() is set to KeyEvent.META_FN_ON)

you can look the package android.text.method to look for a InputMethod. but it's quite complicated.

and there is also a simple way to process.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public boolean onKeyDown(int keyCode, KeyEvent event) {
  3.                 KeyCharacterMap kmap = KeyCharacterMap
  4.                                 .load(event.getDeviceId());
  5.                 int c = kmap.get(keyCode, event.getMetaState());
  6.                 if (c != 0) {
  7.                         char txt = ((char) c);
  8.                         // TODO: process the txt
  9.                 }
  10.                 return false;
  11.         }
  12.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it's not a standard way. i think it'll cause some problems, but it works fine on QWERTY keyboard.

regards
mmin18
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sun Feb 03, 2008 2:33 pm
Location: China

Postby res » Fri Feb 15, 2008 4:25 pm

plusminus wrote:Hello res,

you had a typo "return return" in your post. ;)


ah oops, sorry about that.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests