Search code examples
blackberryjava-mekeylistenerkeyloggerkey-events

BlackBerry - KeyListener with global scope


I am new to BlackBerry App development. I want to be able to listen for keypress events whenever the BlackBerry (8900 in my case) is on and on all screens is this possible?

If so, it would be great for someone to direct me in the right direction. I am already having a look at Interface KeyListener.

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

Thanks all


Solution

  • 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.