我是黑莓应用程序开发的新手。我想在黑莓(我的情况是8900)上所有屏幕都能监听按键事件,这可能吗?
如果可以的话,希望有人能指导我正确的方向。我已经在查看Interface KeyListener。
import net.rim.device.api.system.*;
感谢大家。
我是黑莓应用程序开发的新手。我想在黑莓(我的情况是8900)上所有屏幕都能监听按键事件,这可能吗?
如果可以的话,希望有人能指导我正确的方向。我已经在查看Interface KeyListener。
import net.rim.device.api.system.*;
实现一个类KeyListener,如下:
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;
}
}
然后在你的应用程序构造函数中
public Application() {
//Add the listener to the system for this application
addKeyListener(new ShortcutHandler());
}
我确认当应用程序在后台运行时它是有效的。
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();
}
};
}
这是我想象中的工作方式
UiApplication
甚至Application
的应用程序Keylistener
的实现(如果需要,也可以扩展Thread
)addKeyListener()
将您的KeyListener
实现添加到您的应用程序中