BlackBerry - Key AreaListener with a global scope

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

If so, it would be great if someone directed me in the right direction. I am already looking at the KeyListener interface.

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

Thank you all

+5
source share
4 answers

Implement keylistenerClass as:

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 works when the application is in the background.

+6
source

, , , .
, .

? - Abs 11

, , . RIM OS , , alert, audio, player ..

:
alt text

:

  • go background
  • - > -
  • ,

:

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

, , , . , , . . . , , , . .

+1

All Articles