20

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

Fran Fitzpatrick
  • 17,902
  • 15
  • 33
  • 34

7 Answers7

30

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

Community
  • 1
  • 1
Manuel Barbe
  • 2,104
  • 16
  • 21
  • 2
    You can also use `adb shell input text "text"`, where the text can contain `%s` to represent spaces. – piojo Feb 12 '14 at 11:16
8

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

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • This is great, though I had to put similar lines in the script to escape things like semicolons and ampersands. – Pistos Jul 16 '18 at 18:30
  • 1
    I made a Ruby REPL so you can type freely without worrying about shell escaping: https://gist.github.com/Pistos/0bf26f46c04bc43cc95c224d264e9f39 – Pistos Jul 16 '18 at 18:35
  • 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 ``` – Borderless.Nomad Jan 15 '21 at 17:23
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'"
Giso Bartels
  • 102
  • 1
  • 3
  • 5
    this is not working for me. The following (as suggested above) works adb shell input text "insert%syour%stext%shere" – user985366 Mar 10 '15 at 18:47
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
Fonic
  • 2,625
  • 23
  • 20
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': '你好世界 '}"
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

You can see how it's done in talkmyphone.

They are using Jabber, but it might be useful for you.

Macarse
  • 91,829
  • 44
  • 175
  • 230
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.