Copy the text from the clipboard to Android devices via ADB
Hello! Have you encountered the desire to copy some text on a device lying next to it? I would like it to be as easy as copy-paste to the emulator - it is boring to type hands and not always convenient.
And what about the hotkey: you press it, and the text from the PC clipboard itself starts to be typed on the screen of your phone / tablet - it sounds good, right?
In this article we will talk about using adb as a text copying tool and how it can be made convenient.
If you are an experienced adb user, and you have your own script of this kind - I advise you to go to the implementation itself and share your thoughts on this in the comments.
What and why
We will make a small script that allows you to quickly type the contents of the clipboard on a real device:
This is useful if:
- Check the work with links or enter a new ip to configure the proxy on the device once again:
http://example.com:8080/foo192.168.1.100
- Check the input of special characters or enter tricky test data:
v3rY$ecUrEP@s$w0rD
- Just had to enter a long line:
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:
- Or a test key:
BUexKeGHiERJ4YYbd2_Tk8XlDMVEurbKjWOWvrY
Of course, you probably don’t have to do it regularly. But from time to time I had cases where I had to manually enter something not very convenient for this on the device.
In the end, most often it was the test data or api settings, and the script, although it did not save 5 minutes, but did the work much nicer.
But as?
This can be done using ADB (Android Debug Bridge) . Probably, all developers and most of QA are familiar with it at least thanks to the ability to view logs inside Android Studio or directly, through adb logcat
. If you have not used adb before, you can see an example of installing on macOS here.
We are interested in a command adb shell input
that allows input, for example, tap
or swipe
.
It also allows you to send text - it will start typing in the input field, which is now in focus:
adb shell inputtext <text>
If you enter spaces, they must be replaced by %s
, and special characters zaskeypit. Otherwise, nothing happens.
It is worth considering that adb works only with the Latin alphabet, numbers and special characters from the ASCII nameplates, and input is somewhat limited:
- Does not work with type characters
±§
- Does not work with line breaks (But you can, for example, separately call a line break with another adb
adb shell input keyevent 66
(enter) command or as described here ) - Does not work with Cyrillic
There is an interesting workaround for entering such characters, perhaps it will be possible to fasten the clipboard to it and then print any text. True to the device, you will need to pre-install .apk with a keyboard.
Important: In the above described form of the adb command, it is assumed that one device is connected. If there are several, you can do the following:
1) Call the team for a specific device. The option -s
Find out the device number can be a team adb devices
. Then use the number when calling the command:
$ adb devices
List of devices attached
023db62er9dd7d2b device
$ adb -s 023db62er9dd7d2b shell input text qwe
2) Call a command on a single device connected via usb - option -d
:
adb -d shell inputtext qwe
3) Call a command on a single active emulator - option -e
:
adb -e shell inputtext qwe
More details can be read here .
If you work with several devices and these cases about you, then correct the adb command accordingly.
Implementation
Consider in detail the solution for macOS, but for other systems there is also a way:
- GNU / Linux
- MacOS
- Windows
Linux solution
At one time, the guys from KODE ( Dima Suzdalev and Dima Haiduk ) made an excellent solution for Linux and shared it with me.
It works through the X11 buffer (if you have a Wayland , read below) - you select the text for the bet, and then press the hotkey that calls the script.
Add such a hotkey is easy, you need:
1) Put xclip
2) Add a script file
#!/bin/bash
adb shell input text `xclip -o`
3) Write the path to the script in the Shortcuts settings for the keyboard
Thanks guys and great respect.
Important: The solution above works for X11 (Xorg). For Wayland, this solution is not relevant. I could not find a way to get the contents from the buffer in Wayland, judging by my search for such a possibility yet. Correct, if not right.
If you do not know what kind of environment you have, look here. Most likely, you have X11 and everything will work.
MacOS solution
For macOS linux, the solution did not come up, so I tried to make a similar script that would simplify the call adb shell input text <text>
.
At once I will tell - work with sed is not obvious to me. I tried to put together one team and add a little different recurring replacements that would help correctly extract special characters.
If you figure out how to improve this script, it will be very cool!
It looks like this:
source ~/.bash_profile
adb shell input text $(pbpaste | sed -e 's/[_<>|&$;()\"]/\\&/g' -e 's/ /\%s/g' -e 's/!+/!/g')
(it is source ~/.bash_profile
added if adb works in a regular console, but through Automator (more on this later) adb is not recognized, for this you first need to pull up the path to adb - for example, it is written in ~/.bash_profile
.)
Works as usual adb shell input text <text>
, but
- The source of the text is
pbpaste
- ie the macOS clipboard. - sed processes the clipboard text.
_<>&$;()\"
Escape characters : -&
->\&
- Spaces are replaced with the special character:
` ->
% s` - With an exclamation point, everything is difficult - if someone explains to me such a substitute
!
for!
helping the team not to fall down - it will be cool.
Windows solution
Unfortunately ( or not ) on Windows, I did not try to do this. The most obvious option that comes to my mind is to adapt the solution and install Cygwin . This will allow you to have a convenient linux terminal on hand, which will probably come in handy.
You will need a package sed
and dependencies to it and a package cygutils-extra
(provides a command to get the contents of the clipboard - getclip
for replacement pbpaste
)
The result will be very similar to the macOS solution:
adb shell input text $(getclip | sed -e 's/[_<>|&$;()\"]/\\&/g' -e 's/ /\%s/g' -e 's/!+/!/g')
In Windows 10, it is also possible to get a Linux terminal out of the box . I have not tried this option, but it should be similar to the solution with Cygwin.

Script in action
Simplify work
You can copy the script to the console each time or tamp with your hands adb shell input text <something>
, but this is not very convenient. Easier to make alias or assign hotkey.
About alias for the console
Here the difficulty is that in the alias itself you will need to still copy everything $
and "
make it work. I haven’t done it yet, because the hotkey is more convenient for me. True, he used this before - alias adp='adb shell input text'
who helped type one word of the type adp example
. If someone makes himself alias with a script, write - attach it here.
About hot key that will run the script
If we talk about the Linux solution - it all depends on the distribution, but it is also not difficult.
Cygwin's Windows solution is the easy way.
Initially, the article was for internal use, so the method for macOS is described in more detail, you can see it below:
There are many options for how to do this, but it is installed by default Automator
- you can quickly make a hot key with it.
To begin, run Automator
, select the type of document Service
:

Then we configure the service:
- for
service receives
setno input
- inside the tab
Actions
choose actionRun shell script

Теперь на новый service можно назначить хоткей:

Все, теперь копирование на девайс должно работать по хоткею.
Правда service по хоткею будет работать только в приложениях, где в меню приложения есть вкладка Services
:

В приложении Zeplin для macOS такой вкладки нет, поэтому там это не работает. Возможно, другие приложения для использования скриптов могут обойти это ограничение, мне пока хватало способа через Automator.
Стоит также учесть, что хоткей может перехватить тот же Google Chrome или другое приложение и выполнить свое действие вместо скрипта.
That's all
I hope the article will be useful and will help you to simplify the solution of such problems in your work.