How to generate KeyEvent for two buttons? That is, to generate a combination of two buttons, for example Alt and G.

I try the following methods, but it does not work:

public boolean dispatchKeyEvent(KeyEvent event) { Log.d("KeyEvent","event= "+event); if (event.getAction() != KeyEvent.ACTION_UP){ switch (event.getKeyCode()){ case KeyEvent.KEYCODE_F10: case KeyEvent.KEYCODE_PROG_GREEN: { new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_G | KeyEvent.KEYCODE_ALT_LEFT); return false; } case KeyEvent.KEYCODE_F9: case KeyEvent.KEYCODE_PROG_RED:{ dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_H | KeyEvent.KEYCODE_ALT_LEFT)); return false; } case KeyEvent.KEYCODE_F11: case KeyEvent.KEYCODE_PROG_YELLOW:{ simulateKey(KeyEvent.KEYCODE_ALT_LEFT & KeyEvent.KEYCODE_Y); return false; } case KeyEvent.KEYCODE_F12: case KeyEvent.KEYCODE_PROG_BLUE:{ simulateKey(KeyEvent.KEYCODE_ALT_LEFT | KeyEvent.KEYCODE_B); return false; } } return true; } return super.dispatchKeyEvent(event); } public static void simulateKey( final int KeyCode) { new Thread() { @Override public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(KeyCode); } catch (Exception e) { Log.e("Exception", e.toString()); } } }.start(); } 
  • So how to generate, or how to register? Please clarify. - isnullxbh
  • I myself do not really understand, but when you press the button on the console, for example Up, you need the system to understand the press of a combination of buttons, for example ALT and B. - Salut Amigo
  • one
    Ie, as I understand it, you just need to generate keystrokes for some event (which you will somehow handle yourself), yes? - isnullxbh
  • Yes, yes, it is, you need to generate a click (event) - Salut Amigo
  • pomojete ?????? - Salut Amigo

2 answers 2

 public static void simulateKey(final int KeyCode) { new Thread() { @Override public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(KeyCode); } catch (Exception e) { Log.e("Exception when sendKeyDownUpSync", e.toString()); } } }.start(); } 

Because You need a key combination, then I think you should pass the B code with the META_ALT_ON mask. I think something like that.

UPD: META_ALT_ON | KEYCODE_B META_ALT_ON | KEYCODE_B

  • Grateful, I'll see and let you know. - Salut Amigo
  • If it doesn't bother you, please describe in more detail what exactly this code does. I'm just new in this field :) thanks in advance - Salut Amigo
  • As I understood from the instructions, sendKeyDownUpSync () - sends both KeyUp and KeyDown (that is, pressing and releasing the button) to the active window. - Salut Amigo
  • Simulates keystrokes on the keyboard, you pass the button code - it simulates its keystrokes. It sounds trite - but that is. And what did you ask for) - isnullxbh
  • In theory, it should have been like this - they squeezed Cntrl , squeezed B , let go of B , let go of Cntrl . Here you transmit KeyCode already with "control" using META_ALT_ON | KEYCODE_B META_ALT_ON | KEYCODE_B . - isnullxbh

In the end, solved the problem. It turned out everything was very simple.

 KeyEvent newEvent = new KeyEvent(event.getDownTime(), event.getEventTime(), KeyEvent.ACTION_UP, KeyEvent.KEYCODE_G, event.getRepeatCount(),KeyEvent.META_ALT_RIGHT_ON | KeyEvent.META_ALT_ON, event.getDeviceId(), 0, event.getFlags()); newEvent.dispatch(this); return super.dispatchKeyEvent(newEvent); ), event.getEventTime (), KeyEvent.ACTION_UP, KeyEvent.KEYCODE_G, event.getRepeatCount (), KeyEvent.META_ALT_RIGHT_ON | KeyEvent.META_ALT_ON, event.getDeviceId (), KeyEvent newEvent = new KeyEvent(event.getDownTime(), event.getEventTime(), KeyEvent.ACTION_UP, KeyEvent.KEYCODE_G, event.getRepeatCount(),KeyEvent.META_ALT_RIGHT_ON | KeyEvent.META_ALT_ON, event.getDeviceId(), 0, event.getFlags()); newEvent.dispatch(this); return super.dispatchKeyEvent(newEvent);