Search code examples
javaandroideventstouch

Can I programatically generate a touch event in Android from background app on keypress?


What I want to do is when I press a certain keyboard button or combination, a touch event should be fired with given screen coordinates.

I would like to use a physical keyboard connected to an Android device. I want to write code which runs in the background continuously and listens for a specific combination and then when it is pressed a touch event is created with specific coordinates. The touch event should be received by whatever app which is in foreground.

To be more specific, I find it annoying that the Facebook Messenger app does not send messages when I press enter but inserts a newline in the textbox. Or at least it would be nice to have an option to enable or disable this feature, like the Windows Phone version of the app. But it does not have this option, and it drives me crazy when I type my messages on the physical keyboard and have to touch the little button on the phone itself, moving my hand from the keyboard, or I have to use tabbing navigation to select the button control and then press enter.

This app would primarily be for personal use, and I am asking this question to make sure that it is even possible to write such code and to improve my knowledge.


Solution

  • Apps cannot generate arbitrary touch events, except perhaps using superuser privileges on rooted devices. You are welcome to use the accessibility framework to create an AccessibilityService, but its input options are very limited.