I have a problem with soft keyboard backspace in Android (4.2).
I have a custom editor in a WebView (CodeMirror), which uses an empty <textarea> internally. It seems that backspace is not sent by an Android system unless it believes there is some text in the <textarea>.
I have overridden WebView onCreateInputConnection in an attempt to dumb down soft input:
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
Log.d("CustomWebView", "onCreateInputConnection(...)");
BaseInputConnection connection = new BaseInputConnection(this, false);
outAttrs.inputType = InputType.TYPE_NULL;
outAttrs.imeOptions = EditorInfo.IME_ACTION_NONE;
outAttrs.initialSelStart = -1;
outAttrs.initialSelEnd = -1;
return connection;
}
However, this does not work, and even onKeyUp is not called for backspace.
How do I force soft keyboard to always send backspace?
Ok, finally figured this out.
In Android 4.2 (maybe in earlier versions as well) the backspace is not sent as a
sendKeyEvent(..., KeyEvent.KEYCODE_DEL)by the standard soft keyboard. Instead, it is sent asdeleteSurroundingText(1, 0).So the solution in my case is to make a custom
InputConnectionwith the following:Note: Please let me know if I am doing something stupid here, as it is my 3rd day writing for Android.