I'm writing an IME. As you'd expect, it extends InputMethodService.
In onCreate(), I'm obtaining a KeyCharacterMap:
keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
I need a way to inject phantom keypresses for arbitrary Unicode characters. My first stab looked something like this:
public void generatePhantomKeypress(char[] c) {
KeyEvent[] keySequence = charMap.getEvents(c);
for (KeyEvent k : keySequence)
getCurrentInputConnection().sendKeyEvent(k);
}
It works for letters, digits, and some punctuation marks, but it only seems to work for characters you can enter directly (without deadkey-gymnastics or using the alt keypad) on a PC keyboard.
Some specific characters that cause charMap.getEvents(...) to return null when passed a char[] with a single element whose value is among the following:
- Euro sign '€' (\u20ac)
- Sterling sign '£' (\u00a3).
- It also chokes on accented characters, like 'é' (\uc3a9)
TL/DR: given a char whose value is some arbitrary Unicode value, how do you convert it into a KeyEvent (or sequence of KeyEvent objects) that, when passed to sendKeyEvent(), will cause Android to act as if you entered that character directly from a phantom keyboard?