BlackBerry - Simulate a KeyPress event

好久不见. 提交于 2019-11-27 14:14:52

A kind of hack...

  • start Camera App
  • in TimerTask check if Camera App started and if it need to be closed (some flag)
  • if yes, invoke it(so it will became active) and push ESC keypress event injection to close it

Take a look at this:

class Scr extends MainScreen {
    boolean killCameraApp = false;
    final String mCameraModuleName = "net_rim_bb_camera";
    final CameraArguments args = new CameraArguments();

    public Scr() {
        super();

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                if (isCameraRunning() && killCameraApp) {
                    getApplication().invokeAndWait(callCamera);
                    getApplication().invokeAndWait(killCamera);
                }
            }
        }, 0, 100);
    }

    Runnable callCamera = new Runnable() {
        public void run() {
            callCamera();
        }

    };

    Runnable killCamera = new Runnable() {
        public void run() {
            injectKey(Characters.ESCAPE);
            killCameraApp = false;
        }
    };

    private boolean isCameraRunning() {
        boolean result = false;
        ApplicationManager appMan = 
                ApplicationManager.getApplicationManager();
        ApplicationDescriptor[] appDes = appMan.getVisibleApplications();
        for (int i = 0; i < appDes.length; i++) {
            result = mCameraModuleName.equalsIgnoreCase(appDes[i]
                    .getModuleName());
            if (result)
                break;
        }
        return result;
    }

    private void callCamera() {
        Invoke.invokeApplication(Invoke.APP_TYPE_CAMERA, 
                new CameraArguments());
    }

    private void injectKey(char key) {
        KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
        inject.post();
    }

    protected void makeMenu(Menu menu, int instance) {
        menu.add(new MenuItem("start camera", 0, 0) {
            public void run() {
                callCamera();
                killCameraApp = false;
            }
        });
        menu.add(new MenuItem("kill app", 0, 0) {
            public void run() {
                killCameraApp = true;
            }
        });
        super.makeMenu(menu, instance);
    }
}

EDIT: Don't forget to set permissions for device release:
Options => Advanced Options => Applications => [Your Application] =>Edit Default permissions =>Interactions =>key stroke Injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!