15

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..

Community
  • 1
  • 1
Erandir
  • 389
  • 1
  • 3
  • 7

3 Answers3

24

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.

Eric Tang
  • 241
  • 2
  • 3
10

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... :)

Robert1968
  • 189
  • 1
  • 10
3

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: àèìòù

update 2022

https://stackoverflow.com/a/71367206/236465 shows another solution using AndroidViewClient/culebra and CulebraTester2-public backend.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • While this seems to work for the accented letters you list, I don't think it works for German umlauts such as äöü. Do you know what key combinations to use to produce these? – FD_ May 09 '16 at 14:13
  • 1
    @FD_ Hi, Finally I found the answer. Firstly, on this page, [Key Map](https://source.android.com/devices/input/key-character-map-files) you can know the unicodes of combining diacritical dead key characters. '\u0300': Grave accent. à '\u0301': Acute accent. á '\u0302': Circumflex accent. â '\u0303': Tilde accent. ã '\u0308': Umlaut accent. ä and on this page [Virtual.kcm](https://cs.android.com/android/platform/superproject/+/master:frameworks/base/data/keyboards/Virtual.kcm) you can know how the unicode can be constructed. – Shawn.G Apr 28 '21 at 09:54