Reading USB gamepad events (button press etc.) from an application in view (focus) might not be an issue on Android. The problem is that I need these events from a persistent application/service/etc. which is not necessary in view (e.g. a foreground service). By example, when I press a certain button on the gamepad, a predefined application always should get launched, regardless of the application in view. That is, the application or process which have to receive and process the gamepad events should be persistent, specifically it never can get killed and it should keep processing the events even if loses focus.
As I understand there are neither easy nor elegant ways to do this with a physical keyboard as I have to use an accessibility service (out of question) or write my own input method (complicated and also it has its drawbacks). Maybe is there a better workaround for a gamepad? Note that the device we’re speaking about is actually a Trinket-based USB universal “button pad” which can be easily programmed to act as any HID device like gamepad, keyboard, mouse etc. Even so prefer gamepad over keyboard in order to avoid interfering with the system’s native soft keyboard (but also would be happy if I got a solution for keyboards).
Delphi Firemonkey solution would be the best but of course I’m ready to use C++, Python, Java, etc. instead if necessary. It has to work on Android 7.1 or above (at least Oreo is mandatory).
UPDATE To be a bit more clear let me show what I actually want to do. The android device in point of fact is a touch-only car head unit. I made an active frame with buttons which can act like a gamepad or physical keyboard and have the purpose to start (or switch to) media player, navigation, phone, radio etc. on the head unit (as a touch-only device can distract the driver hence is not really safe at driving time). Of course all buttons should work every time, no matter which application is currently in view.