How can I send key events in android?

Sergei Vasilenko picture Sergei Vasilenko · Oct 23, 2012 · Viewed 41.4k times · Source

I am macking a custom navigation bar to Android 4.0.3.r1 and want to send key events like "Home" and "Back". My application is not a system therefore:

IWindowManager mWindowManager = IWindowManager.Stub.asInterface(
                ServiceManager.getService(Context.WINDOW_SERVICE));
mWindowManager.injectKeyEvent( ev, false );

It doesn't work, because I can not get android.permission.INJECT_EVENTS from not system application. How can I do this?

Answer

Roman Trokhymets picture Roman Trokhymets · Aug 13, 2013
BaseInputConnection  mInputConnection = new BaseInputConnection(targetView, true);
mInputConnection.sendKeyEvent(new KeyEvent(...));