views:

2272

answers:

5

Hello all,

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

A: 

This how I imagine it could work

jitter
Your keyListener would only get key events when your application is in the foreground.
Richard
which isn't true but thanks for the downvote
jitter
+1  A: 
Max Gontar
How does the volume up and down key work?
Abs
Thank you for the explanation and code - it helps me understand all this.
Abs
You're welcome!
Max Gontar
+1  A: 

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.

Ali El-sayed Ali
Wow - thank you for confirming that, I will try it out.
Abs
A: 

Hi,

I am trying to do a similar implementation where I can display the letter that I have typed in a picture form. Below is the code I have implemented but it does not work. Can you advise me on where I have gone wrong?

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 abc extends UiApplication implements KeyListener {

    Scr mScreen;

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

    public static void main(String[] args) {
        abc abcGame = new abc();
        abcGame.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) {
        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;
    }
}

class Scr extends MainScreen {
    int letter;
    LabelField abcLabel = new LabelField("The Letter : "
                + String.valueOf(letter));

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

    public void addLine(final String text) {
        getApplication().invokeLater(new Runnable() {
                public void run() {
                        abcLabel.setText("The Letter : "
                                        + String.valueOf(letter));
                        insert(new LabelField(text), 1);
                }
        });
    }


}
dk
A: 

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.

dexter