简体   繁体   English

Android:如何在软键盘上触发任何按键事件?

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

Is there any way to press key of Android Soft Keyboard programmatically. 有没有办法以编程方式按下Android软键盘的键。

Like: When keyboard will appear, I want to press "J" key through my code not from fingers. 就像:当键盘出现时,我想通过代码而不是手指按“ J”键。

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. 您会看到这个问题 ,该问题解释了如何使用检测和Webview来执行此操作。

You don't need the keyboard to do this, you can show it or not. 您不需要键盘即可执行此操作,您可以显示或不显示它。

A list of keyCodes if you want. 如果需要,列出keyCode

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. 链接将为您按下的每个键显示keyCode,我认为它可与android和linux键盘一起使用,但不知道代码在其他操作系统下是否相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM