1

In webview or default browser, I can receive backspace key events(keyup, keydown and keypress) only when the input is not empty, It seems like it's a bug of android 4.1+.

You can try this page below(open it using android devices) http://javascript.info/tutorial/keyboard-events#test-stand-test-stand

I tried the answer here, and here, by creating a custom InputConnection.

It's working now, I can receive backspace key events even if the input is empty, but there are side effects:

I can't input words and phrase any more(the 1st and 2nd picture),

I can only input one letter or character at a time(the 3rd picture).

enter image description here enter image description here enter image description here

How can I fix this, is there any workaround? Thanks.

Community
  • 1
  • 1
Doctor.Who.
  • 607
  • 1
  • 7
  • 15

2 Answers2

3

I don't know about your case but in my case qwerty keyboard is having backspace keydown/keyup events but numeric is not triggering backspace key events my android version is 4.4.2 And I have modified the code you used from those 2 posts you mentioned like this. Just use this code and your backspace keyevents will be fired

This is the extended webview class where we are defining our own onCreateInputConnection and ExtenderInputConnection is my class which extends base input connection and its code is also given below

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    super.onCreateInputConnection(outAttrs);
    //This Code is for showing Decimal point in numeric Keyboard
    if ((outAttrs.inputType & InputType.TYPE_CLASS_NUMBER) == InputType.TYPE_CLASS_NUMBER)
    {
        outAttrs.inputType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
    }
    else
    {
        InputConnection connection = super.onCreateInputConnection(outAttrs);
        return connection;
    }
     return new ExtenderInputConnection(this,false);
}

Here is the code for ExtenderInputConnection

import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

public class ExtenderInputConnection extends BaseInputConnection implements InputConnection{

public ExtenderInputConnection(View targetView, boolean fullEditor) {
    super(targetView, fullEditor);
    // TODO Auto-generated constructor stub
}

@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {       
    if (beforeLength == 1 && afterLength == 0) {
        // backspace
        return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
    }

    return super.deleteSurroundingText(beforeLength, afterLength);
    }
}
Syed Wajahat Ali
  • 541
  • 1
  • 7
  • 25
  • this fixed the Go vs Next button for me, however it breaks the Next button for numeric inputs. Any ideas? – spy Jul 14 '15 at 00:28
0

Do you need to use onkeyup/keydown/keypress events? They don't fit the mobile input model very well which is why these bugs exist. Would composition events be a better solution for you[1]?

[1] https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent

ksasq
  • 4,424
  • 2
  • 18
  • 10
  • Thanks for your anwser.I've try compositionEvents in this page using my android phone, but there were no compositeEvents fired. http://jsfiddle.net/A6Ctj/5/ – Doctor.Who. Apr 30 '14 at 13:01