title: Injecting non-English characters in Android
body: <p>I am programming on a remote control app. One of the tasks is injecting characters. The code I am currently using looks like this:</p>
  
  <pre><code>Instrumentation instr = new Instrumentation();
  
  String str="a";
  
  // basically the same like calling instr.sendStringSync(str);
  char[] chars = str.toCharArray();
  KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
  KeyEvent[] keyEvents = keyCharacterMap.getEvents(chars);
  if (keyEvents != null) {
      for (KeyEvent kev : keyEvents) {
          instr.sendKeySync(kev);
      }
  }
  </code></pre>
  
  <p>That works perfectly on English characters (The characters show up in EditText boxes). However, if I am trying to inject e.g. Korean characters, this fails. The function getEvents returns null, even when I have configured Korean language and keyboard.</p>
  
  <p>I know there is another method for injecting strings directly:</p>
  
  <pre><code>KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), str, 0, 0);
  instr.sendKeySync(event);
  </code></pre>
  
  <p>This is not working either - no characters shown in EditText boxes, and onKeyMultiple() is not called either in my test activity.</p>
  
  <p>This is strange since dispatchKeyEvent() with the same event works in my test activity:</p>
  
  <pre><code>KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), str, 0, 0);
  dispatchKeyEvent(event);
  </code></pre>
  
  <p>My remote control app needs to inject events no matter to which activity. This is possible using Instrumentation (with <code>android.permission.INJECT_EVENTS</code> and a signature with the platform key).</p>
  
  <p>How can I inject non-English characters using instrumentation?
  Is there another way to accomplish this? E.g. Using dispatchKeyEvent (has to work for other activities/apps as well).</p>