开发者

Recognise which key is pressed in bb

I am trying to recognise which key is pressed and do the action as required. Basically using them to perform zoom in n zoom out on开发者_运维知识库 the press of "i" n "o".

I have used these methods:

protected boolean keyDown(int keycode, int time)
{           
    int key=Keypad.key(keycode);
    String keyC=Integer.toString(key);
    System.out.println("*********************************   key pressed"+key);
    System.out.println("*********************************   key pressed to string"+keyC);
    return super.keyDown(keycode, time);
}

public boolean keyChar(char key, int status, int time) 
{
    System.out.println("inside keychar");
    boolean retval = false;
    int zoom=mapField.mf.getZoom();
    if(key== 'o'||key== 'O')
    {
        zoom=zoom-3;
        mapField.mf.setZoom(zoom);

        retval = true;
    }
    super.mf.setZoom(zoom);
    return retval;
}

these methods dont seem to work at all.


Ok let me give you how I would do it and hopefully there will be an aha moment somewhere.

    import net.rim.device.api.system.KeyListener;
    import net.rim.device.api.ui.Keypad;
    // any other imports you might need

    public final class VivartClass implements KeyListener {

    public boolean keyChar(char key, int status, int time) 
        {
            System.out.println("inside keychar");
            if(key== 'o'||key== 'O')
            {
                int zoom=mapField.mf.getZoom();
                zoom=zoom-3;
                mapField.mf.setZoom(zoom);
                super.mf.setZoom(zoom);
                return true;


            }
            return super.keyChar(key, status, time);
       }

       protected boolean keyDown(int keycode, int time) {

            int key=Keypad.key(keycode);
            String keyC=Integer.toString(key);
            System.out.println("*********************************   key pressed"+key);
            System.out.println("*********************************   key pressed to string"+keyC);
            return super.keyDown(keycode, time);
        }

        }

Then in your application constructor

        public Application() {
            addKeyListener(new VivartClass());
            // all your other stuff you may want to do
    }

"apps should use the keyChar notification to determine which characters a user has pressed" is the recommendation from www.blackberry.com in their API.

Also, make sure there are no other keyChar methods lying around elsewhere in your code. If there are, then this one you are expecting to be called will not be called.

Also instead of key=='o' try to use some of the values from net.rim.device.api.system.Characters to see if there are some keys you can get for example

key == Characters.LATIN_SMALL_LETTER_O 

Oh, one last try. You could see if

Keypad.getAltedChar(key) == 'o'

improves your situation as well.

Sorry I have neither a simulator nor a device in front of me at the moment so I cannot run these with you, but hopefully I have hit on the issue with one of these.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜