Reputation: 11933
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!
Upvotes: 2
Views: 2578
Reputation: 1939
Use keyChar to find the character, but in order to capture regardless of whether the character is alted, you would also need to use Keypad.getAltedChar:
public boolean keyChar(char key, int status, int time) { if (key == '2' || Keypad.getAltedChar(key) == '2') { // Do your stuff here. } }
Upvotes: 3
Reputation: 14562
Rather than keyDown(), override keyChar() instead, like so:
public boolean keyChar(char key, int status, int time)
{
switch (key)
{
case Characters.DIGIT_TWO:
//do something
return true;
}
return super.keyChar(key, status, time);
}
The Characters class gives you access to a well-known set of characters
Upvotes: 0