Android: Backspace in WebView / BaseInputConnection

I have a problem with soft keyboard transition in Android (4.2).

I have my own editor in WebView (CodeMirror) that uses an empty <textarea> inside. It seems that the backspace is not sent by the Android system unless it considers there is text in the <textarea> .

I disabled WebView onCreateInputConnection in an attempt to drown out 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 not called for backspace.

How to make soft keyboard always send backspace?

+15
android android-input-method android-4.2-jelly-bean
Jan 28 '13 at 10:48
source share
2 answers

Ok, finally figured it out.

In Android 4.2 (possibly in earlier versions) the backspace is not sent as sendKeyEvent(..., KeyEvent.KEYCODE_DEL) standard soft keyboard. Instead, it is sent as deleteSurroundingText(1, 0) .

So the solution in my case is to create a custom InputConnection with the following:

 @Override public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { // backspace return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } 

Note. Please let me know if I am doing something stupid here, since this is my third day for Android.

+31
Jan 28 '13 at 11:47
source share

this code will be better, it will work with a larger keyboard: D

 @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { outAttrs.actionLabel = null; outAttrs.inputType = InputType.TYPE_NULL; final InputConnection con = new BaseInputConnection(this,false); InputConnectionWrapper public_con = new InputConnectionWrapper( super.onCreateInputConnection(outAttrs), true) { @Override public boolean deleteSurroundingText(int beforeLength, int afterLength) { if (beforeLength == 1 && afterLength == 0) { return this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && this.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } @Override public boolean sendKeyEvent(KeyEvent event) { if(event.getKeyCode() == KeyEvent.KEYCODE_DEL){ return con.sendKeyEvent(event); }else { return super.sendKeyEvent(event); } } }; try { return public_con ; }catch (Exception e){ return super.onCreateInputConnection(outAttrs) ; } } 
+2
Oct 27 '16 at 6:09
source share



All Articles