I am programming on a remote control app. One of the tasks I am facing
is injecting characters. The code I am currently using looks like
this:

    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 = m_KeyCharacterMap.getEvents(chars);
    if (keyEvents != null) {
                for (KeyEvent kev : keyEvents) {
                               instr.sendKeySync(kev);
                }
    }

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.

I know there is another mehtod for injecting strings directly:

    KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), str, 0, 0);
    instr.sendKeySync(event);

This is not working either - no characters shown in EditText boxes,
and onKeyMultiple() is not called either in my test activity.

This is strange since dispatchKeyEvent() with the same event works in
my test activity:

    KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), str, 0, 0);
    dispatchKeyEvent(event);

I believe injecting the string directly is the correct way. However,
using instrumentation it looks like the event is discarded somewhere
in Android.

The reason why I am not using dispatchKeyEvent is that I have to
inject events no matter which activity is in front, even it is not my
own activity. I think that is not possible using dispatchKeyEvent, is
it?
Using Instrumentation this is possible. At least with english
characters. (My app is signed with the platform key and I've got the
permission INJECT_EVENTS).

How am I supposed to inject non-english characters? Any ideas? I ran
out of ideas :(

Best Regards,
Hardy Kahl

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to