How To Copy Text From The Clipboard To Android Devices Via ADB

Copy Text From The Clipboard Main Logo

How To Copy Text From The Clipboard To Android Devices Via ADB

Hello! Did you encounter the desire to copy some text to the nearby device? We would like it to be just as easy as copy-paste on the emulator – it’s boring to type hands and it’s not always convenient.

And what about HotKey: press it, and the text from the clipboard of the PC begins to be typed on the screen of your phone/tablet – it sounds good, right?

In this article, we’ll talk about using Android Debug Bridge as a text copy tool and how it can be made convenient.

If you are an experienced user of Android Debug Bridge, and you have your own script of this kind – we advise you to go to the implementation itself and share your thoughts on this in the comments.

What is for?

We will make a small script that will allow you to quickly type the contents of the clipboard on a real device:

Copy Text From The Clipboard Photo 8

This is useful if:

  • Check the work with links or enter a new ip to configure the proxy on the device once again:



192.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 test key:

BUexKeGHiERJ4YYbd2_Tk8XlDMVEurbKjWOWvrY

Of course, most likely you do not have to do this regularly. But from time to time, we had cases where it was necessary to enter manually on the device something not too convenient for this.

In the end, most of the time it was test data or api settings, and although the script did not save 5 minutes, it made the work much more enjoyable.

How are we going to do this?

This can be done using ADB (Android Debug Bridge). Probably, all developers and most QA are familiar with it at least thanks to the ability to view logs inside Android Studio or directly, via ADB logcat. If you have not used ADB before, you can see an example of installing on macOS here.

We are interested in the adb shell input command, which allows input, for example, tap or swipe.

It also allows you to send a text – it will start typing in the input field, which is now in focus:


adb shell input text <text>

If you enter spaces, you need to replace them with% s, and special characters for secrecy. Otherwise, nothing happens.

It should be noted that ADB works only with the Latin alphabet, numbers and special characters from the ASCII tablet, and the input is somewhat limited:

  • Does not work with symbols of type ± g
  • Does not work with line wrapping (But you can for example separately cause the row to be transferred by another ADB command adb shell input keyevent 66 (enter) or as described here)
  • Does not work with Cyrillic

There is an interesting workaround for entering such characters, perhaps it can be attached to the clipboard and then already print any text. True to the device will need to reinstall. APK with the keyboard.

Important: In the above and the following, the ADB command implies that one device is connected. If there are several, you can do the following:

1) Call the command to a particular device. The -s option
You can find out the device number using the adb devices command. Then use the number when you call the command:


$ adb devices
List of devices attached
023db62er9dd7d2b device
$ adb -s 023db62er9dd7d2b shell input text qwe

2) Call the command to a single device connected via USB – the -d option:


adb -d shell input text qwe

3) Call the command on the only active emulator – the -e option:


adb -e shell input text qwe

More details can be read here.

If you work with several devices and these cases are about you, correct the ADB command accordingly.

Implementation

Let’s take a closer look at the solution for macOS, but for other systems, there is also a way:

  • GNU/Linux
  • MacOS
  • Windows

Solution for Linux

Copy Text From The Clipboard Photo 7

At one time, the guys from KODE made an excellent solution for Linux and shared it with us.

It works through the X11 buffer (if you have Wayland, read below) – you select text for the bet and then click the hotkey that calls the script.

Add this hotkey is easy, you need:

  • Supply xclip.
  • Add a file with the script.
  • Prescribe the path to the script in the shortcuts for the keyboard.

#!/bin/bash
adb shell input text `xclip -o`

Important: The solution above works for X11 (Xorg). For Wayland, this solution is not relevant. We still could not find a way to get content from the buffer in Wayland, judging by my search for such an opportunity 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.

Solution for macOS

Copy Text From The Clipboard Photo 2

For macOS Linux, the solution did not work, so we tried to make a similar script that would simplify the call to adb shell input text <text>.

At once we will tell – work with sed for me is not obvious. We tried to assemble in one team and add a few regulars of substitutions, which would help properly disassemble the 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')

(source ~ / .bash_profile is added if in the usual console ADB works, but through Automator (more about this later) ADB is not recognized, for this, you first need to pull up the path to ADB – for example, written in ~ / .bash_profile.)

It works like usual adb shell input text <text>, but

  1. The source of the text is pbpaste – that is, the macOS clipboard.
  2. sed processes the text from the clipboard.
  3. The characters _ <> & $; () \ “are escaped: – & -> \ &
  4. Spaces are replaced with a special character: `->% s`
  5. With an exclamation point it’s hard – if someone explains such a replacement ! on ! helps the team not to fall – it will be cool.

Solution for Windows

Copy Text From The Clipboard Photo 1

Unfortunately (or not) on Windows we did not try to do that. The most obvious option that comes to my mind is to adopt the solution and put Cygwin. This will allow you to have a convenient Linux terminal handy, which is probably useful.
You will need the sed package and the dependencies to it and the cygutils-extra package (provides the command to get the contents of the clipboard – getclip to replace pbpaste)

The result will be very similar to the solution for macOS:


adb shell input text $(getclip | sed -e 's/[_<>|&$;()\"]/\\&/g' -e 's/ /\%s/g' -e 's/!+/!/g')

In Windows 10, you can also get a Linux terminal out of the box. This option was not tried, but it should look like a solution with Cygwin.

Simplify work

You can copy the script to the console each time, or stuff it with adb shell input text <something>, but it’s not too convenient. It’s easier to make the alias or assign a hotkey.

About alias for the console

Here, the difficulty is that an alias you will need to sketch all the $ and to make it work.” we have not done this yet since we prefer the hotkey.) Before that we used alias adb = ‘adb shell input text’ which helped me type one word like adb example. If someone makes the alias with the script, write – attach here.

About the hotkey that will run the script

If to speak about Linux the decision – all depends on the distribution kit, but it too is not difficult.

  • The solution for Windows on Cygwin 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:

Method for macOS

There are many options to do this, but Automator is installed by default – you can quickly make a hotkey with it.

To start, start Automator, select the document type Service:

Copy Text From The Clipboard Photo 3

Then configure the service:

  • For service receives we put no input
  • Inside the Actions tab, select the Run shell script action

Copy Text From The Clipboard Photo 4

Now you can assign a hotkey to the new service:

Copy Text From The Clipboard Photo 5

All, now copying to the device should work on the hotkey.

True service on hotkey will work only in applications wherein the application menu there is a tab Services:

Copy Text From The Clipboard Photo 6

In the Zeplin application for macOS, there is no such tab, so there it does not work. Perhaps other applications for using scripts can bypass this limitation, we still had a way through the Automator.

  • It is also worth noting that the hotkey can intercept the same Google Chrome or another application and perform its action in place of the script.

That’s all

We hope this article will be useful and will help you to simplify the solution of such problems in your work.