黑莓 - 全局范围内的KeyListener

5

我是黑莓应用程序开发的新手。我想在黑莓(我的情况是8900)上所有屏幕都能监听按键事件,这可能吗?

如果可以的话,希望有人能指导我正确的方向。我已经在查看Interface KeyListener。

import net.rim.device.api.system.*;

感谢大家。
4个回答

6

实现一个类KeyListener,如下:

import model.Profile;


(注:该代码为Java语言。)
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.Keypad;


public final class ShortcutHandler implements KeyListener {

    public boolean keyChar(char key, int status, int time) {
        return false;
    }

    public boolean keyDown(int keycode, int time) {
        if (Keypad.KEY_ESCAPE == Keypad.key(keycode)) {
                        // Consume the event.
                        // Here I'm consuming the event for the escape key
            return true;
        }
                //let the system to pass the event to another listener.
        return false;
    }

    public boolean keyRepeat(int keycode, int time) {
        return false;
    }

    public boolean keyStatus(int keycode, int time) {
        return false;
    }

    public boolean keyUp(int keycode, int time) {
        return false;
    }

}

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

public Application() {

    //Add the listener to the system for this application
    addKeyListener(new ShortcutHandler());
}

我确认当应用程序在后台运行时它是有效的。


3
据我理解,您想要监听设备上所有运行应用程序的所有关键事件,而不仅限于您的应用程序。 我认为这是不可能的。 更新 如果您想说所有应用程序都可以接收音量键的关键事件,那就不是真的。 RIM OS会接收这些事件,然后更新所有音频组件,如警报、音频、播放器等。
您可以通过以下示例轻松检查它:
alt text
操作步骤:
1. 运行示例 2. 输入一些关键事件 3. 查看事件编号 4. 切换到背景 5. 输入一些关键事件 6. 通过菜单->切换应用程序返回示例 7. 检查事件编号,它仍然是相同的
代码:
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;

public class KeyListenerApp extends UiApplication implements KeyListener {

    Scr mScreen;

    public KeyListenerApp() {
        mScreen = new Scr();
        pushScreen(mScreen);
        addKeyListener(this);
    }

    public static void main(String[] args) {
        KeyListenerApp app = new KeyListenerApp();
        app.enterEventDispatcher();
    }

    private void updateScreen(final String text) {
        mScreen.addLine(text);
    }

    public boolean keyChar(char key, int status, int time) {
        updateScreen("keyChar " + key);
        return true;
    }

    public boolean keyDown(int keycode, int time) {
        updateScreen("keyDown " + keycode);
        return true;
    }

    public boolean keyRepeat(int keycode, int time) {
        updateScreen("keyRepeat " + keycode);
        return true;
    }

    public boolean keyStatus(int keycode, int time) {
        updateScreen("keyStatus " + keycode);
        return true;
    }

    public boolean keyUp(int keycode, int time) {
        updateScreen("keyUp " + keycode);
        return true;
    }
}

class Scr extends MainScreen {
    int mEventsCount = 0;
    LabelField mEventsStatistic = new LabelField("events count: "
            + String.valueOf(mEventsCount));

    public Scr() {
        super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
        add(mEventsStatistic);
    }

    public void addLine(final String text) {
        getApplication().invokeLater(new Runnable() {
            public void run() {
                mEventsStatistic.setText("events count: "
                        + String.valueOf(++mEventsCount));
                insert(new LabelField(text), 1);
            }
        });
    }

    protected void makeMenu(Menu menu, int instance) {
        super.makeMenu(menu, instance);
        menu.add(goBGMenuItem);
    }

    MenuItem goBGMenuItem = new MenuItem("go backgroun", 0, 0) {
        public void run() {
            getApplication().requestBackground();
        }
    };
}

1
上面给出的代码确实有效,但有一个问题。您将无法在原生应用程序(如呼叫处理短信接收浏览等)上捕获按键。因为系统会向这些应用程序生成全局事件。就像当您的应用程序在后台时能够定义单击例程一样,但该例程的功能仅限于您的应用程序。它不会对其他应用程序产生影响。

1

你的 keyListener 只会在你的应用程序处于前台时获取键事件。 - Richard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接