Tony Lin
Tony Lin

Reputation: 805

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. Does anyone know how to listen to the soft keyboard?

Upvotes: 4

Views: 5101

Answers (2)

Augusto Gonzalez
Augusto Gonzalez

Reputation: 246

I was having the same problem. But what I did was used a library that is already done. Check this link: 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.

Upvotes: 1

scottt
scottt

Reputation: 8371

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.

"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."

I think using the TextWatcher interface is probably as close as the you're going to get to what you want.

Upvotes: 12

Related Questions