Invoke keypress event in android?

若如初见. 提交于 2019-12-21 05:58:17

问题


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


回答1:


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.




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


来源:https://stackoverflow.com/questions/3361281/invoke-keypress-event-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!