Invoke keypress event in android?

2019-04-02 19:46发布

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

2条回答
闹够了就滚
2楼-- · 2019-04-02 19:59

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);
查看更多
欢心
3楼-- · 2019-04-02 20:08

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.

查看更多
登录 后发表回答