直接从计算机在 Android 设备上打字?

问题描述 投票:0回答:6

可以使用 ADB 从电脑直接在 Android 设备上打字吗?如果是的话,怎么办?

android adb
6个回答
32
投票

虽然这个问题相当老了,但我想添加这个答案:

您可以分别使用

adb shell input keyevent KEYCODE
adb shell input text "mytext"
。所有键码的列表可以在here

找到

9
投票

正如Manuel所说,你可以使用

adb shell input text
,但你需要用
%s
替换空格,并处理引号。这是一个简单的 bash 脚本,可以让这一切变得非常简单:

#!/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"

另存为,例如,

atext
并使其可执行。然后您可以调用不带引号的脚本...

atext Hello world!

...除非您需要发送引号,在这种情况下您确实需要将它们放在其他类型的引号之间(这是 shell 限制):

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


5
投票

为了避免扩展/评估文本参数(即对于“$”或“;”等特殊字符),您可以将它们括在引号中,如下所示:

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

2
投票

这是一个基于

Bash
的解决方案,适用于任意/复杂字符串(例如随机密码)。在这方面,这里提出的其他解决方案对我来说都失败了:

#!/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

以下代码可用于重复/连续输入:

#!/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

2
投票

input
不支持UTF-8或其他编码,如果你尝试的话你会看到这样的东西

$ adb shell input text ö
Killed

因此,如果这是您的意图,您需要更强大的东西。

以下脚本使用 AndroidViewClient/culebraCulebraTester2-public 后端来避免

input
限制。

#! /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, '你好世界 😄')

它找到一个

EditText
,然后输入一些汉字和表情符号。

如果您只需要输入文本,您可以使用

bash
curl
实现相同的目的。

#! /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': '你好世界 😄'}"

0
投票

使用 zsh 时,这里有一个更强大的功能来向 Android 提供文本:

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

虽然 Zsh 引用可能与常规 POSIX shell 略有不同,但我没有发现任何它不能工作的地方。丹的答案缺失,例如

>
也需要转义。

我正在将它与

pass show ... | adbtext
一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.