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

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:

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 optionYou can find out the device number using the adb devices command. Then use the number when you call the command:

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

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!

(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

The source of the text is pbpaste – that is, the macOS clipboard.

sed processes the text from the clipboard.

The characters _ <> & $; () \ “are escaped: – & -> \ &

Spaces are replaced with a special character: `->% s`

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

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)

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.

Paul Brook is a professional full stack developer & IT content marketer. Developer by day and a content Marketer by night. He is the lead Developer and content marketer at www.smartspate.com. His passion for helping people in all aspects IT and Marketing flows through in the expert industry coverage he provides. In addition to IT, also provide technical SEO and copywriting services for business of all sizes.

Find Us At:

error: This Content is Protected! All Rights belong to Smart Spate Ltd.

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. For information on a cookie and how it impacts on users, you can visit our Privacy Policy and Cookie Policy.AcceptCloseRead more