How to determine which key was pressed on the BlackBerry

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

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

I managed to get this to work in what seems to be a workaround by looking at the key code "2" with the ALT button activated 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 have looked at the constants defined in KeypadListener

and Keypad

, but I cannot find any constants mapped to the actual buttons on the device.

Will a more experienced BlackBerry developer provide assistance?

Thanks!

+2


a source to share


2 answers


Use keyChar to find the character, but in order to capture regardless of whether the character is set up you will 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. 
    } 
}
+3


a source


Instead of keyDown () instead of overriding keyChar (), like this:

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 Symbols class gives you access to a known set of symbols

0


a source







All Articles