Search code examples
androidgesturekeyeventd-pad

How to send a KeyEvent if a gesture is detected


I want a gesture D-Pad, but how can I send an e keyevent if I detect a gesture? For example, I detect the gesture down, and now I want to send the keyevent KEYCODE_DPAD_DOWN.

How can i do that?


Solution

  • If you want to send the key event only to your application, the easiest is to just inject it in the root view:

    KeyEvent event = new KeyEvent(...)
    someView.getRootView().dispatchKeyEvent(event);
    

    If you want to send it to another application/window, then you cannot (you would need a special permission, which requires you to have your application signed with the system certificate, AFAIK)