문제

저는 BlackBerry App Development를 처음 사용합니다. BlackBerry (8900)가 켜져 있고 모든 화면에서 이것이 가능할 때마다 Keypress 이벤트를들을 수 있기를 원하십니까?

그렇다면 누군가 나를 올바른 방향으로 안내하는 것이 좋을 것입니다. 이미 인터페이스 Keylistener를 살펴보고 있습니다.

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

감사합니다

도움이 되었습니까?

해결책

KeylistenerClass와 같은 카일 리스너 사이드를 구현하십시오.

가져 오기 모델 .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());
}

응용 프로그램이 백그라운드에있을 때 작동하는지 확인합니다.

다른 팁

내가 이해했듯이, 당신은 응용 프로그램에서뿐만 아니라 장치에서 실행되는 모든 응용 프로그램의 모든 주요 이벤트를 듣고 싶습니다.
나는 그것이 불가능하다고 생각합니다.

업데이트

볼륨 상승 및 다운 키는 어떻게 작동합니까? - 11 시간 전에 ABS

모든 응용 프로그램이 볼륨 키에서 주요 이벤트를 수신한다고 말하고 싶다면 사실이 아닙니다. Rim OS는 이러한 이벤트를 수신 한 다음 Alert, Audio, Player 등과 같은 모든 오디오 구성 요소를 업데이트합니다.

이 샘플로 쉽게 확인할 수 있습니다.
alt text

다음을 수행하십시오 :

  • 샘플 실행
  • 몇 가지 주요 이벤트를 입력하십시오
  • 이벤트 번호를보십시오
  • 배경
  • 몇 가지 주요 이벤트를 입력하십시오
  • 메뉴-> 스위치 응용 프로그램으로 샘플로 돌아갑니다
  • 이벤트 번호를 확인하십시오. 여전히 동일합니다

암호:

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 네가 원한다면)
  • 당신의 추가 KeyListener 응용 프로그램을 통해 구현 addKeyListener()
  • 그런 다음 원하는대로하십시오.

위에 주어진 코드는 확실히 작동하지만 캐치가 있습니다. 통화 처리 SMS 수신 브라우징 및 물건과 같은 기본 앱에서 키 프레스를 가두 지 못할 것입니다. 시스템 이이 앱에 대한 글로벌 이벤트를 생성함에 따라. 앱이 백그라운드에있을 때 클릭에 대한 루틴을 정의 할 수 있지만 해당 루틴의 기능은 응용 프로그램에만 국한됩니다. 다른 앱에 영향을 미치지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top