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.