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

发布于 2024-09-28 14:43:47 字数 50 浏览 5 评论 0原文

您可以使用 ADB 直接从计算机在 Android 设备上打字吗?如果是这样,怎么办?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

予囚 2024-10-05 14:43:47

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

您可以使用 adb shell input keyevent KEYCODE 分别。 adb shell 输入文本“mytext”。所有键码的列表可以在此处找到

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

雪落纷纷 2024-10-05 14:43:47

正如Manuel所说,您可以使用adb shell输入文本,但需要将空格替换为%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"'

在此处输入图像描述

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

东京女 2024-10-05 14:43:47

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

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

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'"
べ映画 2024-10-05 14:43:47

这是一个基于 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

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
浅听莫相离 2024-10-05 14:43:47

input 不支持 UTF-8 或其他编码,如果您尝试的话,您会看到类似的内容,

$ adb shell input text ö
Killed

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

以下脚本使用 AndroidViewClient/culebraCulebraTester2-public 后端以避免输入限制。

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

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': '你好世界 ????'}"
夜清冷一曲。 2024-10-05 14:43:47

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

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

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

我将它与 pass show ... | 一起使用adbtext

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文