[android-developers] Re: how to inject non-english characters
Hi LeenuxGuy,
I found kind of a solution:
1. Backup clipboard content
2. Put your character in clipboard
3. Inject menu-key, then "v"-key. This is a keyboard shortcut for
pasting the clipboard.
4. Unfortunately Android is inserting spaces before and after the
content in some cases.
Try to remove them by injecting DPAD_LEFT, DELETE....
5. Put the backed up clipboard content back in the clipboard.
That is in fact so ugly and unreliable, that I discarded it right away.
* On some phones the paste shortcut also triggers the menu.
* There is a racecondition in the clipboard. Sometimes the old
clipboard content is pasted instead of the just inserted character.
* I can't get rid of these spaces reliably. I don't know why Android
is doing this. That's even a pain when using the clipboard the
intended way.
So, currently I stick with english characters. Sorry.
I would be glad to hear from you, when you got a solution.
Best regards,
Hardy
2012/9/18 LeenuxGuy <bijoy.anose@gmail.com>:
> Hi Hardy,
>
> Did you ever find a solution for this? I also have a signed app with
> INJECT_EVENTS permissions, and I want to be able to send any character
> (English or otherwise) on to various EditText fields, based on external
> input.
>
>
>
> On Tuesday, May 29, 2012 3:06:43 AM UTC-5, Hardy Kahl wrote:
>>
>> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home