Question

I have a custom view with onKeyDown() overridden. It works fine while I use standard Android english (or italian) soft keyboard, but when I set the device to Russian language key events never reach onKeyDown() or onKeyUp() (they're never called, I tried with a breakpoint).

Moreover, if all views discarded the event, I should be able to catch it from the activity's methods, but it seems I can't even there. Note that the view is focused, in fact I can catch directional key presses. Also the delete/backspace russian soft key is catched.

How am I supposed to catch russian character key events?

Was it helpful?

Solution

From the KeyEvent docs:

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.

To the extent you are getting KeyEvents, it's against the recommendation of Google and is not supported in all versions of android going forward. That approach is certain to break on more devices as time goes on. So how do you capture characters? One option is to use a TextWatcher.

OTHER TIPS

This seems to be an issue that other Android users have had as well, and the solution that I found is to override dispatchKeyEvent instead.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    Toast.makeText(getApplicationContext(), "Key: " + event.getKeyCode(),
        Toast.LENGTH_LONG).show();

    return super.dispatchKeyEvent(event);
}

And using the event.getAction() call can tell you if it's onKeyDown, onKeyUp, etc. For more information on KeyEvents check out this Android developers' page and for more information about Android keyboards not responding to certain languages check out this bug report.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top