Home » Android » android – adb shell input unicode character

android – adb shell input unicode character

Posted by: admin June 15, 2020 Leave a comment

Questions:

Knowing the basic key mappings described in ADB Shell Input Events I get the emulation of text input and special keys working quite well. But what about Unicode characters? For instance I want to use umlauts from the German QWERTZ keyboard layout.

This gets me:

$ adb shell input text ö
Killed

So it seems to crash and

adb shell input text \xFC

prints xFC on the input. I have tried to the the events with getevent but I haven’t found a direct mapping, I’ve also looked into the keymapping file /system/usr/keylayout/Qwerty.kl

I believe the only possibility is via the clipboard, but as pointed out in the question Pasting text into Android emulator clipboard using adb shell it seems to be unknown how to use it for Android Ice Cream Sandwich or later..

How to&Answers:

input won’t work because it can only send single key event through the virtual keyboard (check the source code if you don’t know what I mean).

I think the only way left is using Instrumentation. I guess you can create a test for your Activity and then do something like this:

                final Instrumentation instrumentation = getInstrumentation();
                final long downTime = SystemClock.uptimeMillis();
                final long eventTime = SystemClock.uptimeMillis();

                final KeyEvent altDown = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_GRAVE, 1, KeyEvent.META_ALT_LEFT_ON);
                final KeyEvent altUp = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_UP,
                        KeyEvent.KEYCODE_GRAVE, 1, KeyEvent.META_ALT_LEFT_ON);

                instrumentation.sendKeySync(altDown);
                instrumentation.sendCharacterSync(KeyEvent.KEYCODE_A);
                instrumentation.sendKeySync(altUp);
                instrumentation.sendKeySync(altDown);
                instrumentation.sendCharacterSync(KeyEvent.KEYCODE_E);
                instrumentation.sendKeySync(altUp);
                instrumentation.sendKeySync(altDown);
                instrumentation.sendCharacterSync(KeyEvent.KEYCODE_I);
                instrumentation.sendKeySync(altUp);
                instrumentation.sendKeySync(altDown);
                instrumentation.sendCharacterSync(KeyEvent.KEYCODE_O);
                instrumentation.sendKeySync(altUp);
                instrumentation.sendKeySync(altDown);
                instrumentation.sendCharacterSync(KeyEvent.KEYCODE_U);
                instrumentation.sendKeySync(altUp);

This will send the modified keys: àèìòù

Answer:

I wrote a virtual keyboard that accept broadcast intent, so you can send unicode characters to the editText view via adb.

for e.g.
adb shell am broadcast -a ADB_INPUT_TEXT –es msg “你好嗎! Hello!”

Here is the github project:
https://github.com/senzhk/ADBKeyBoard

Hope this little project would help.

Answer:

Actually ADBKeyBoard is very good! Thanks for Eric Tang !

Some useful extensions for comfortable usage:

Switch to ADBKeyBoard from adb:

   adb shell ime set com.android.adbkeyboard/.AdbIME   

Check your available le virtual keyboards:

ime list -a  

Use simple quote characters -not double as in example above- if your shell not accepts “!” (explanation sign)

adb shell am broadcast -a ADB_INPUT_TEXT --es msg 'Accented characters here'

Switch back to original virtual keyboard: (swype in my case…)

adb shell ime set com.nuance.swype.dtc/com.nuance.swype.input.IME  

Use adb over wifi to simplify your life… 🙂

Answer:

You should turn on USB simulated input.
The set path is : (Open Setting -> Developer options -> USB simulated input)