问题
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
回答1:
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.
回答2:
I think you can extend webview and override public boolean onKeyPreIme(int keyCode, KeyEvent event)
回答3:
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
来源:https://stackoverflow.com/questions/12730635/cannot-receive-keycode-del-on-webview-in-android-4-1-1