BlackBerry - KeyListener with global scope

梦想的初衷 提交于 2019-12-05 11:28:11
Ali El-sayed Ali

Implement a keylistenerClass like:

import model.Profile;

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

}

Then in your application constructor

public Application() {

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

I confirm that it's working when the application is in the background.

Maksym Gontar

As I understood, you want to listen to all key events in all applications running on device, not only in your application.
I think it's not possible.

UPDATE

How does the volume up and down key work? – Abs 11 hours ago

If you want to say that all applications receive key events from volume keys, thats not true. RIM OS will receive those events and then update all audio components like alert, audio, player etc.

you can easely check it with this sample:

Do following:

  • run sample
  • enter some key events
  • look at events number
  • go background
  • enter some key events
  • go back to sample by menu->switch application
  • check events number, it still the same

Code:

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

This how I imagine it could work

The code given above certainly works, but there is a catch. You wont be able to trap the key presses on native apps like call handling sms incoming browsing and stuff. As system generates global event to these apps. Its like you are able to define a routine for clicks when your app is in background , but the functionality of that routine is localised to your application only. It wont effect other apps as such.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!