识别 bb 中按下的是哪个键

发布于 2024-11-18 00:21:58 字数 799 浏览 2 评论 0原文

我试图识别按下了哪个键并根据需要执行操作。基本上使用它们在按“i”和“o”时执行放大和缩小。

我用过这些方法:

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;
}

这些方法似乎根本不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-11-25 00:21:58

好吧,让我告诉你我会怎么做,希望在某个地方会有一个顿悟的时刻。

    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);
        }

        }

然后在您的应用程序构造函数中

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

“应用程序应使用 keyChar 通知来确定用户按下了哪些字符”是 www.blackberry.com 在其 API 中的建议。

另外,请确保代码中的其他位置没有其他 keyChar 方法。如果有,那么您期望被调用的这个将不会被调用。

另外,不要使用 key=='o' 尝试使用 net.rim.device.api.system.Characters 中的一些值来查看是否有一些您可以获得的键,例如

key == Characters.LATIN_SMALL_LETTER_O 

哦,一个最后一次尝试。您可以看看是否

Keypad.getAltedChar(key) == 'o'

也能改善您的情况。

抱歉,目前我面前既没有模拟器也没有设备,所以我无法与您一起运行这些程序,但希望我已经解决了其中一个问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文