BlackBerry - Simulate a KeyPress event

前端 未结 1 1681
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 09:39

I have a BlackBerry application that needs to take pictures from the camera and send them to a server. In order to do this i invoke the native camera application and listen

相关标签:
1条回答
  • 2020-12-03 10:13

    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

    0 讨论(0)
提交回复
热议问题