3

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

Trung Nguyen
  • 7,442
  • 2
  • 45
  • 87

3 Answers3

4

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.

Lior Hass
  • 91
  • 1
  • 4
  • You are awesome, this solition helped me a lot – Nifhel Mar 29 '13 at 17:03
  • 1
    Does not work for API level 19, Built Target level 19. The deleteSurroundingText() method simply does not get called at all (Nexus 7 2012 running Android 4.3, default soft keyboard). – Carl Dec 09 '13 at 09:59
0

I think you can extend webview and override public boolean onKeyPreIme(int keyCode, KeyEvent event)

cheng yang
  • 1,692
  • 3
  • 12
  • 21
  • built target 19 doesn't seem to call this either (nexus 7 2012 android 4.4) – yarbelk Feb 20 '14 at 05:20
  • Not a complete solution. Selecting all the text in javascript seems to let me start deleting on the second press of the backspace key. – yarbelk Feb 21 '14 at 02:31
0

Try to capture 'input' event on KEYCODE_DEL.

$('#textBox').on('input', function(event) {
    console.log(event, event.keyCode, event.which);
    // it will capture the event, but you will not get the event.keyCode 
    // and event.which.
});

For more Details - backSpace not firing the keyup event in android mobile or JQuery: detect change in input field

Community
  • 1
  • 1
viks
  • 1,368
  • 16
  • 19