1

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?

Bitbang3r
  • 6,826
  • 5
  • 27
  • 40

1 Answers1

1

Normally an IME doesn't generate key events. It usually uses inputConnection.commit() to send data to the application. That API takes any CharSequence (which can even include limited styling).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Can you use inputConnection.commit() if you aren't using prediction? (I'm creating an IME that uses the buttons on a bluetooth mouse to enter Morse(-ish) code) – Bitbang3r Feb 01 '17 at 07:13
  • Yes. The input connection has nothing to do with prediction. Its a communication channel between the View that is connected to the keyboard and the keyboard app. – Gabe Sechan Feb 01 '17 at 07:14
  • @Bitbang3r , Would you be kind and be able to share a snippet of the final implementation . I am struggling with a similar scenario , It is easy to do control characters but anything beyond basic latin and punctuation seems out of reach . – theDarkerHorse Oct 30 '20 at 05:44