Android: How to trigger any key pressing event on soft keyboard?

ぃ、小莉子 提交于 2019-12-08 01:04:31

问题


Is there any way to press key of Android Soft Keyboard programmatically.

Like: When keyboard will appear, I want to press "J" key through my code not from fingers.


回答1:


First method:

IBinder binder = ServiceManager.getService("window"); 
IWindowManager manager = IWindowManager.Stub.asInterface(binder);
manager.injectKeyEvent(new KeyEvent(KeyEvent.yourAction, KeyEvent.yourKeyCode),true);

You can see more details here. There is another method in this link too.

Second method, using instrumentation:

Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);

And you can see this question that explains how to use instrumentation and webview to do that.

You don't need the keyboard to do this, you can show it or not.

A list of keyCodes if you want.

This link will show keyCode for every key that you press, I think it works with the android and linux keyboards, but don't know if the code will be the same using another OS.



来源:https://stackoverflow.com/questions/35751231/android-how-to-trigger-any-key-pressing-event-on-soft-keyboard

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