viv
viv

Reputation: 6177

Invoke keypress event in android?

I need to invoke a key press event in android . Any suggestions ?????

Upvotes: 2

Views: 3525

Answers (2)

anon
anon

Reputation:

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);

Upvotes: 2

Sephy
Sephy

Reputation: 50392

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.

Upvotes: 5

Related Questions