Can you use ADB to type directly on an android device from a computer? If so, how?
6 Answers
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
1 Comment
adb shell input text "text", where the text can contain %s to represent spaces.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"' 3 Comments
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 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
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
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
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.

inout: stackoverflow.com/a/14273689/236465