How to create key clicks of programs

In my application, when the user presses DPAD_LEFT, I want to generate two hits of DPAD_UP. I know that this can be done using the following method:

@Override private boolean onKeyDown(int keyCode, KeyEvent event) {

   if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {

        keyDownUp(KeyEvent.KEYCODE_DPAD_UP);

        keyDownUp(KeyEvent.KEYCODE_DPAD_UP);

        return true;

   }
   return super.onKeyDown(keyCode,event);
}

private void keyDownUp(int a) {

        getCurrentInputConnection().sendKeyEvent(

                new KeyEvent(KeyEvent.ACTION_DOWN, a));

        getCurrentInputConnection().sendKeyEvent(

                new KeyEvent(KeyEvent.ACTION_UP, a));

}

But, being able to use the getCurrentInputConnection () method, I need to extend InputMethodService, and this is not possible because my application is already extending another class. Is there any other way to solve this problem?

+3
source share
4 answers

Create another class that extends InputMethodService and calls it from your application.

+4
source

, (, , , ), . -, . - :

public final static int DISPATCH_KEY_FROM_IME = 1011;

KeyEvent evt = new KeyEvent(motionEvent.getAction(), keycode);
Message msg = new Message();
msg.what = DISPATCH_KEY_FROM_IME;
msg.obj = evt;

anyViewInTheHierarchy.getHandler().sendMessage(msg);

:)

+1
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
+1

( , ), , - :

@Override private boolean onKeyDown(int keyCode, KeyEvent event) {

   if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
        super.onKeyDown(KeyEvent.KEYCODE_DPAD_UP,event);  
        return super.onKeyDown(KeyEvent.KEYCODE_DPAD_UP,event);  
   }
   return super.onKeyDown(keyCode,event);
}

, .

0

Source: https://habr.com/ru/post/1791432/


All Articles