开发者

Typing on an Android Device straight from computer?

开发者_如何学C

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


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


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"'

Typing on an Android Device straight from computer?


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'"


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


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, '你好世界 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜