How to figure out which key was pressed on a BlackBerry

Posted by Skrud on Stack Overflow See other posts from Stack Overflow or by Skrud
Published on 2010-04-22T20:55:31Z Indexed on 2010/04/23 22:53 UTC
Read the original article Hit count: 270

Filed under:
|
|
|

What I want: To know when the user has pressed the button that has the number '2' on it, for example. I don't care whether "Alt" or "Shift" has been pressed. The user has pressed a button, and I want to evaluate whether this button has '2' printed on it.

Naturally, if I switch devices this key will change. On a Bold 9700/9500 this is the 'E' key. On a Pearl, this is the 'T'/'Y' key.

I've managed to get this working in what appears to be a roundabout way, by looking up the keycode of the '2' character with the ALT button enabled and using Keypad.key() to get the actual button:

// figure out which key the '2' is on:
final int BUTTON_2_KEY = Keypad.key(KeypadUtil.getKeyCode('2', KeypadListener.STATUS_ALT, KeypadUtil.MODE_EN_LOCALE));

protected boolean keyDown(int keycode, int time) {
    int key = Keypad.key(keycode);
    if ( key == BUTTON_2_KEY ) {
       // do something
       return true;
    }
    return super.keyDown(keycode,time);
}

I can't help but wonder if there is a better way to do this. I've looked at the constants defined in KeypadListener and Keypad but I can't find any constants mapped to the actual buttons on the device.

Would any more experienced BlackBerry devs care to lend a helping hand?

Thanks!

© Stack Overflow or respective owner

Related posts about blackberry

Related posts about java