I need to invoke a key press event in android . Any suggestions ?????
Asked
Active
Viewed 3,438 times
2 Answers
5
You need to use the instrumentation class :
Instrumentation i = new Instrumentation();
i.sendKeyDownUpSync(KeyEvent.KEYCODE_A);
This should be equivalent to a A pressed on the keyboard.

Sephy
- 50,022
- 30
- 123
- 131
-
Thanks for the response will be testing it soon. Hope it will do the job. Thanks again. – viv Jul 29 '10 at 10:14
-
Don't forget that this method is synchronous, so you might also need to wrap it in something asynchronous. – Constantine Ketskalo Oct 18 '19 at 11:47
2
The following example shows how to invoke Key Back key event:
KeyEvent eventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
KeyEvent eventUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK);
dispatchKeyEvent(eventDown);
dispatchKeyEvent(eventUp);