Can't get meta state from Android KeyEvent

2019-09-04 11:56发布

问题:

Looks silly, but I can't get meta state out of KeyEvent, accessed from onKeyListener. Tried with all keyboards I have and with emulators.

Whether or not Shift, Ctrl, etc keys are pressed, keyEvent.getMetaState() returns 0. May it works for TextListener, but I don't need it for entering text, I just want to differentiate between Tab and Shift+Tab.

Thanks for anticipated help.

Update. What I just figured out is that meta state is reported for alphabetic keys, but not for other keys.

For example if I press left Shift+T the system generates KeyEvent for KEYCODE_SHIFT_LEFT and KEYCODE_T, and KeyEvent for KEYCODE_T has META_SHIFT_ON set. You can trick the system with Shift+TAB+T, in which case META_SHIFT_ON is set for both KEYCODE_T and KEYCODE_TAB. However with Shift+TAB the KeyEvent for KEYCODE_SHIFT_LEFT is not generated, and the meta state remains unaffected.

Maybe system keyboard configuration files need to be updated to allow combinations like Shift+TAB?

回答1:

Yes, it's really about android configuration file.

You modify TAB entry in /system/usr/kychars/Generic.kcm (or the file for corresponding vendor) and add a line for shift, as the following:

key TAB {
    label:                              '\t'
    base:                               '\t'
    shift:                              '\t'
    ctrl, alt, meta:                    none
}

Some devices uses qwerty.kcm, which has the shift line alaready present. They should work OK without intrusion. BTW, unicode has provides a special code \u21B9 for Shift+TAB, but it might not be recognised by Android.