简体   繁体   English

在软键盘android上听每个关键事件

[英]listen to every key event on a soft keyboard android

I wan't to listen to the soft keyboard event. 我不想听软键盘事件。 I've tried setOnKeyListener and setKeyListener and both of them don't work. 我已经尝试过setOnKeyListener和setKeyListener,但它们都不起作用。 Does anyone know how to listen to the soft keyboard? 有谁知道如何听软键盘?

Assuming you're using a soft keyboard on a newer Android version, then the following blurb from the KeyEvent reference explains key limitations in getting soft key events. 假设您在较新的Android版本上使用软键盘,则KeyEvent参考中的以下模糊解释了获取软键事件的主要限制。

"As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event : this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method. In particular, the default software keyboard will never send any key event to any application targetting Jelly Bean or later , and will only send events for some presses of the delete and return keys to applications targetting Ice Cream Sandwich or earlier. Be aware that other software input methods may never send key events regardless of the version. Consider using editor actions like IME_ACTION_DONE if you need specific interaction with the software keyboard, as it gives more visibility to the user as to how your application will react to key presses." “由于软输入方法可以使用多种创造性的输入文本方式, 因此无法保证软键盘上的任何按键都会产生关键事件 :这由IME自行决定,实际上不鼓励发送此类事件。您永远不应该依赖于接收软输入法上任何键的KeyEvent。特别是, 默认软件键盘永远不会向任何针对Jelly Bean或更高版本的应用程序发送任何键事件 ,并且只会发送一些按下删除键的事件。将键返回到目标为Ice Cream Sandwich或更早版本的应用程序。请注意,无论版本如何,其他软件输入方法都可能永远不会发送关键事件。如果您需要与软件键盘进行特定交互,请考虑使用编辑器操作,如IME_ACTION_DONE,因为它提供了更多的可见性用户关于你的应用程序如何对按键进行反应。“

I think using the TextWatcher interface is probably as close as the you're going to get to what you want. 我认为使用TextWatcher界面可能就像你要达到你想要的那样接近。

I was having the same problem. 我遇到了同样的问题。 But what I did was used a library that is already done. 但我所做的是使用已经完成的库。 Check this link: androidLibrary 检查此链接: androidLibrary

Just follow the step and you will be done. 只需按照步骤操作即可完成。 Importing the library and then use the code that the provided you. 导入库,然后使用提供给您的代码。

You will just have to do import the dependencies like: 您只需要导入依赖项,如:

dependencies {
    compile 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.0.0'
}

And implement this method: 并实现此方法:

KeyboardVisibilityEvent.setEventListener(
        getActivity(),
        new KeyboardVisibilityEventListener() {
            @Override
            public void onVisibilityChanged(boolean isOpen) {
                // some code depending on keyboard visiblity status
            }
        });

This is the basically the main function. 这基本上是主要功能。

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

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