Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot receive KEYCODE_DEL on WebView in Android 4.1.1

I want to make WebView editable by creating a complex javascript to handle keyboard event. Everything works fine on all Android version except 4.1. In 4.1 I can handle all key events except KeyEvent.KEYCODE_DEL. It seems that we cannot handle KEYCODE_DEL event for WebView in Android 4.1? I am very appreciate if someone can help me on this issue

Thanks

like image 677
Email Booster Avatar asked May 25 '26 05:05

Email Booster


1 Answers

Looks like an Android bug. A simple workaround that worked for me was to set

android:targetSdkVersion="15"

in your AndroidManifest.xml

EDIT:

After some more research, I now think that this is not a bug but rather a deliberate change. The KeyEvent documentation says:

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.

In reality though, it still sends events for most key presses except for the Delete key. Since I really needed all key events I came up with this solution:

First, create your own View (in my case it was derived from TextView) like this:

public class MyTextView extends TextView {

  ...

    @Override
    public InputConnection onCreateInputConnection(EditorInfo editorInfo) {
        editorInfo.actionLabel = null;
        editorInfo.inputType   = InputType.TYPE_NULL;
        editorInfo.imeOptions  = EditorInfo.IME_ACTION_NONE;

        return new MyInputConnection(this, false);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return true;
    }
}

Second, create MyInputConnection by subclassing BaseInputConnection like this:

public class MyInputConnection extends BaseInputConnection { 

    ...


    // From Android 4.1 this is called when the DEL key is pressed on the soft keyboard (and
    // sendKeyEvent() is not called). We convert this to a "normal" key event.
    @Override
    public boolean deleteSurroundingText(int beforeLength, int afterLength) {
        long eventTime = SystemClock.uptimeMillis();
        sendKeyEvent(new KeyEvent(eventTime, eventTime,
                KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE | KeyEvent.FLAG_EDITOR_ACTION));
        sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,
                KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
                KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE | KeyEvent.FLAG_EDITOR_ACTION));
        return true;
    }

In your InputConnection class you've got good control on what's going on. For example you can override the commitText() method to get events about keys of foreign language letters etc.

like image 132
Lior Hass Avatar answered May 26 '26 18:05

Lior Hass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!