Android:如何在软键盘上触发任何按键事件? [英] Android: How to trigger any key pressing event on soft keyboard?

查看:382
本文介绍了Android:如何在软键盘上触发任何按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以通过编程方式按下Android软键盘的键.

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

像:当键盘出现时,我想通过代码而不是手指按"J"键.

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

推荐答案

第一种方法:

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.

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

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.

这篇关于Android:如何在软键盘上触发任何按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆