Embedding non-English characters in Android

I am programming a remote control application. One of the tasks is to enter characters. The code I use is as follows:

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); } } 

This works great on English characters (characters are displayed in EditText blocks). However, if I try to introduce, for example, Korean characters, this fails. The getEvents function returns null even if I configured the Korean language and keyboard.

I know that there is another method for directly entering lines:

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

This also does not work - no characters are displayed in EditText blocks, and onKeyMultiple () is not called 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); 

My remote control application should fire events no matter what action. This is possible using Instrumentation (with android.permission.INJECT_EVENTS and a signature with a platform key).

How can I enter non-English characters using the toolkit? Is there any other way to do this? For example. Using dispatchKeyEvent (should work for other actions / applications).

+7
source share
2 answers

I leave the part above as additional information. I have found a solution. This requires root, but if you can sign the application key, I think this is not a problem. What you can do is edit the Virtual.kcm file (/system/usr/keychars/Virtual.kcm), which is the default key symbol map (kcm). You can add any character you want and then use the Instrumentation.sendStringSync (String string) method, because it can generate KeyEvents from the new kcm.

I had problems editing kcm on the phone, so I did this to copy it to a computer, edit it and then copy it back to the device.

Hope this helps!


The following content will appear in this link . This means that the virtual keyboard has a US keyCharacterMap and layout, regardless of what you choose from the settings. I could not find a way to solve this problem.

 Language Note Android does not currently support multilingual keyboards. Moreover, the built-in generic key character map assumes a US English keyboard layout. OEMs are encouraged to provide custom key character maps for their keyboards if they are designed for other languages. Future versions of Android may provide better support for multilingual keyboards or user-selectable keyboard layouts. 
+2
source

You can use this method and InputFilter for EditText:

 private boolean isLatinSymbolOrDigit(char c) { // Allow [a-zA-Z0-9] if ('0' <= c && c <= '9') return true; if ('a' <= c && c <= 'z') return true; if ('A' <= c && c <= 'Z') return true; return false; } InputFilter filter = new InputFilter() { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if (isLatinSymbolOrDigit(source.charAt(i))) { } else { //wrong character return ""; } } return null; } }; editText.setFilters(new InputFilter[] {filter}); 
-2
source

All Articles