Android WebView HTML input keypress doesn’t fire

On some devices (mostly Samsung, but others too) and combinations of: Android version, WebView version (even with the evergreen WebView in Android 7) and keyboard, there are a number of issues:

  • keypress isn’t fired
  • keydown and keyup always contain keyCode=229
  • keypress and input are fired but don’t contain the key
  • textInput isn’t fired
  • maxlength attribute isn’t honored on input[type=text] while the user types (more than maxlength chars are allowed and the input is validated only when the form is submitted)

Is there a way to fix these issues?

Answer

I found that if you extend WebView and override onCreateInputConnection, all of those issues are fixed:

public class WebViewExtended extends WebView {
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        // This line fixes some issues but introduces others, YMMV.
        // super.onCreateInputConnection(outAttrs);

        return new BaseInputConnection(this, false);
    }
}

Before overriding onCreateInputConnection:

before

After overriding onCreateInputConnection (g was pressed):

after

Leave a Reply

Your email address will not be published. Required fields are marked *