24

Can you use ADB to type directly on an android device from a computer? If so, how?

1

6 Answers 6

36

Although this question is rather old, I'd like to add this answer:

You may use adb shell input keyevent KEYCODE resp. adb shell input text "mytext". A list of all keycodes can be found here

Sign up to request clarification or add additional context in comments.

1 Comment

You can also use adb shell input text "text", where the text can contain %s to represent spaces.
9

As Manuel said, you can use adb shell input text, but you need to replace spaces with %s, as well as handle quotes. Here's a simple bash script to make that very easy:

#!/bin/bash text=$(printf '%s%%s' ${@}) # concatenate and replace spaces with %s text=${text%%%s} # remove the trailing %s text=${text//\'/\\\'} # escape single quotes text=${text//\"/\\\"} # escape double quotes # echo "[$text]" # debugging adb shell input text "$text" 

Save as, say, atext and make executable. Then you can invoke the script without quotes...

atext Hello world! 

...unless you need to send quotes, in which case you do need to put them between the other type of quotes (this is a shell limitation):

atext "I'd" like it '"shaken, not stirred"' 

enter image description here

3 Comments

This is great, though I had to put similar lines in the script to escape things like semicolons and ampersands.
I made a Ruby REPL so you can type freely without worrying about shell escaping: gist.github.com/Pistos/0bf26f46c04bc43cc95c224d264e9f39
You can also do something like, lang-bsh #!/bin/bash escape_text() { printf '%s' "$1" | sed -e 's/[]['\''\"&;()|$]/\\&/g' } text=$(printf '%s%%s' ${@}) # concatenate and replace spaces with %s text=${text%%%s} # remove the trailing %s text=$(escape_text "$text") echo "Text = $text" # debugging
5

To avoid expansion/evaluation of the text parameter (i.e. for special characters like '$' or ';'), you could wrap them into quotes like this:

adb shell "input text 'insert your text here'" 

1 Comment

this is not working for me. The following (as suggested above) works adb shell input text "insert%syour%stext%shere"
2

Here is a Bash-based solution that works for arbitrary/complex strings (e.g. random passwords). The other solutions presented here all failed for me in that regard:

#!/usr/bin/env bash read -r -p "Enter string: " string # prompt user to input string string="${string// /%s}" # replace spaces in string with '%s' printf -v string "%q" "${string}" # quote string in a way that allows it to be reused as shell input adb shell input text "${string}" # input string on device via adb 

The following code may be used for repeated/continuous input:

#!/usr/bin/env bash echo echo "Hit CTRL+D or CTRL+C to exit." echo while true; do read -r -p "Enter string: " string || { echo "^D"; break; } string="${string// /%s}" printf -v string "%q" "${string}" echo "Sending string via adb..." adb shell input text "${string}" done 

Comments

2

input does not support UTF-8 or other encodings, you will see something like this if you try it

$ adb shell input text ö Killed 

therefore if these are your intention you need something more robust.

The following script uses AndroidViewClient/culebra with CulebraTester2-public backend to avoid input limitations.

#! /usr/bin/env python3 # -*- coding: utf-8 -*- from com.dtmilano.android.viewclient import ViewClient vc = ViewClient(*ViewClient.connectToDeviceOrExit(), useuiautomatorhelper=True) oid = vc.uiAutomatorHelper.ui_device.find_object(clazz='android.widget.EditText').oid vc.uiAutomatorHelper.ui_object2.set_text(oid, '你好世界 😄') 

it finds an EditText and then enters some Chinese characters and a emoji.

You can achieve the same using bash and curl if entering text is the only you need.

#! /bin/bash # # simple-input-text # - Finds an EditText # - Enters text # # prerequisites: # - adb finds and lists the device # - ./culebratester2 start-server # - jq installed (https://stedolan.github.io/jq/) # set -e set +x base_url=http://localhost:9987/v2/ do_curl() { curl -sf -H "accept: application/json" -H "Content-Type: application/json" "$@" } oid=$(do_curl -X POST "${base_url}/uiDevice/findObject" \ -d "{'clazz': 'android.widget.EditText'}" | jq .oid) do_curl -X POST "${base_url}/uiObject2/${oid}/setText" \ -d "{'text': '你好世界 😄'}" 

Comments

0

When using zsh, here is a more robust function to feed text to Android:

function adbtext() { while read -r line; do adb shell input text ${(q)${line// /%s}} done } 

While Zsh quoting may be slightly different from a regular POSIX shell, I didn't find anything it wouldn't work on. Dan's answer is missing for example > which also needs to be escaped.

I am using it with pass show ... | adbtext.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.