Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send key events in android?

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?

like image 269
Sergei Vasilenko Avatar asked Oct 23 '12 08:10

Sergei Vasilenko


1 Answers

Here are some precision to Roman answer

BaseInputConnection  mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
like image 76
Lotfi Avatar answered Nov 09 '22 23:11

Lotfi