Can I send keystrokes to my USB-connected Android phone? - android

Is there any generic app for Windows and/or Andorid that enables me to send keystrokes to my USB-connected developer-enbled Android phone? Ideally it would be a toggle button that switches whether to send my keystrokes to Windows or to Android.
My use case is that I have apps that require a bit of text punching and I'd like to do that with my physical keyboard instead of my touch based phone. I do not want to physically connect my keyboard directly to my phone. It is now connected to the PC.

You can use the ADB for that.
You can do it drom the command line - adb shell input text "my text" or from "inside" the decice - open first the shell - adb shell and from the device's command line type input "my text"

adb shell input text "my text" -- will FAIL
Error: Invalid arguments for command: text
usage: input ...
input text
input keyevent
input tap
input swipe
you need to escape the following characters:
( ) < > | ; & * \ ~ " ' 'escaping' means putting a backslash ( \ ) before the offending character.
space is escaped by using %s
adb shell input text "my%stext" -- will work

Related

adb shell input text Android 11 inCall

I have a problem when sending DTMF events inside of an active call.
I am using adb shell input text #0123* to send DTMF tones. This was working good so far for older Android versions. Starting from Android 11 (I didn't check 12 yet), special characters * and # are not working anymore and sending 8 and 3 instead.
If I am sending the text at other text fields, the special chars are formatted correctly, the error is only occurring inside the call application when having an active call and trying to send DTMF tones via dialpad.
Edit:
I tried sending with ' escaped:
User:~$ adb -s RF8N31NZ5SK shell
x1s:/ $ input text '#*'
x1s:/ $
But result is the same:
Edit-2:
It looks like dialer is not allowing special chars anymore on Android >= 11. I managed to do it via keyevents KEYCODE_POUND and KEYCODE_STAR:
User:~$ adb -s RF8N31NZ5SK shell
x1s:/ $ input keyevent KEYCODE_POUND
x1s:/ $ input keyevent KEYCODE_STAR
x1s:/ $
You can try adding apostrophe to surround text to sent because # character is used for comments on Android shell :
adb shell input text '#1234*'
It worked on my side with Android 11
You can escape the hash character and place the whole input text in double quotes:
adb shell input text "\#1234*"
Note: In my tests I had to use double quotes, single quotes were not working, with single quotes the typed text was \#1234*. But this may depend on the used terminal/platform for executing the whole command (in my case Windows 10 command-line/cmd.exe).
This commands works for me and

send keyboard input to firestick via adb

I've been trying to write a batch so I can type on my PC via adb and send input to firestick tv.
I have accomplished this with the following script:
:Start
adb connect 1XX.1XX.1.X1X
set /p intxt=Type...
adb shell input keyboard text "%intxt%"
pause
GOTO Start
But if you send text input to device via adb you need to add %S to enter spaces between words. For example for "Hello World" you need to input Hello%sWorld
Please HELP! I have been trying for week with no results.
This is the closest I have gotten to a solution, but it doesn't seam to work:
set filename=%filename: ="%s"%
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET "intxt=something containing spaces"
set "intxt=!intxt: =%%s!"
SET intxt
ECHO adb shell input keyboard text "%intxt%"
FOR /f %%x IN ("%intxt%") DO ECHO %%x
GOTO :EOF
Seems to work with echo - whether adb sees it the same way is for you to experiment with and report back...

Tell Smartphone to send SMS with PC

Is there a way to do the following instructions using Android on phone and Windows on computer?
Computer: Reads numbers and meeting-dates out of a database.
Computer: Connects to the smartphone (via USB?) and uses its "send SMS"-function
Smartphone: Sends SMS with given Text.
Or do i have to use Third Party Email to SMS tools? I'd like to avoid that.
You can use USB debugging and some faked user input.
First, enable USB debugging. Depending on your Android version and device model, this may be either an option somewhere in the normal settings app or in the hidden developer mode which you can access by tapping the build number under "Status" 7 times. Also, you may be asked to allow access from your computer the first time you try to do anything with this feature.
Then, download ADB and any driver needed for your device, if any. ADB is a command-line tool to send debugging commands to your device.
You can use adb devices in the console to see if things work - you should see your device listed.
The idea is now to start the SMS app with recipient and text already filled in (which is a supported action) and then fake the user clicking "Send". This is where the tricky part lies. Depending on your device, there can be different key input required than for mine, for example. Usually you will need to send one or more D-Pad presses plus "Enter" (yes this works even if the device doesn't have a D-Pad).
The commands you'll need:
adb shell am start -a android.intent.action.SENDTO -d sms:<full phone number here> --es sms_body "<SMS text here>" --ez exit_on_sent true
This will open the SMS app with the values already filled in. Replace <full phone number here> with the phone number, and <SMS text here> with your text. If you need to use quotes inside the text, you have to escape them like \".
adb shell input keyevent <id>
This will send a fake keypress. The possible IDs are listed here. Particularly interesting are:
19: D-Pad up
20: D-Pad down
21: D-Pad left
22: D-Pad right
23: D-Pad center
66: Enter
You will need to play with these. You probably need to send 22 or 20 to simulate a "right" or "down" press to focus the "Send" button followed by 23 or 66 to simulate a press on the D-Pad center or enter key to "click" the button.
So, for example, it could look like this:
adb shell am start -a android.intent.action.SENDTO -d sms:+436501234567 --es sms_body "This is a test" --ez exit_on_sent true
adb shell input keyevent 20
adb shell input keyevent 66
This would open the SMS app with the number +436501234567 and the text This is a test already filled in and then simulate the "down" key and the "enter" key.
Please give the whole thing some time. Add some delays (at least 1s) between each command.

Does "adb shell input text" simulate software keyboard input?

adb shell input text "sometext"
OR
adb shell input keyevent eventid
do these simulate actual input from a virtual/ hardware keyboard respectively ?
I did not find any documentation for these commands on developer.android.com/
Is there any trusted documentation for these commands?
adb shell input help produces (after a long list of input devices):
The commands and default sources are:
text <string> (Default: touchscreen)
keyevent [--longpress] <key code number or name> ... (Default: keyboard)
tap <x> <y> (Default: touchscreen)
swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
press (Default: trackball)
roll <dx> <dy> (Default: trackball)
So it seems "text" is indeed for virtual (Default: touchscreen) and keyevent for physical (Default: keyboard).
You can override the text input device - adb shell input keyboard text "foo" works just fine. You can't send raw keycodes from the screen though.
Related to
adb shell input text "some\ text"
I only find the source code:
E. g. for Android 8.1 here.
The method private void sendText(int source, String text) { is relevant for the encoding.
And searching for KeyCharacterMap.VIRTUAL_KEYBOARD that is used in previous method I found a description here for heading "Virtual Key Character Map File".
From that info I looked into an old SM-G900F. I found under /system/usr/keychars/Virtual.kcm.
Inside of that file it maps the key events to the most common characters (mostly only ASCII).
So to your question:
do these simulate actual input from a virtual/ hardware keyboard
respectively ?
Yes, the code takes the text and tries to map it back via the KeyCharacterMap.VIRTUAL_KEYBOARD to key events and sends them.
In my experience all unknown characters lead to a cancellation of the whole text.
And you have to escape some characters - sometimes space with %s, or with '\ ', other special characters like & have to be escaped too.
And on some devices I experienced that a long text (ca. 200 characters) written with adb shell input text "<longText>" was partly lost - even the bigger part at the end!
It looked to me depending on the manufacturer.
There is a good description available for adb shell input * commands, just type
adb shell input help
For adb shell keyevent commands, different keycodes are available.
I am developing a work that evaluate the performance among different keyboards and tries to simulate real-user keyboard typing. The tool that I am using (android view client) uses input text to send text to the device. However, when using this tool to simulate text input, I observed differences in the behaviour of the keyboards, caused by the usage of this input method.
Using input text, the pointer location doesn't change and the keyboard doesn't show any keypress animations. Contrariwise, when using input tap X Y to press a key, the visual behaviour is the same as a real user taping the key. Also, the behaviour of the GBoard is different for both input methods. When using input text and then tapping a suggested word, the keyboard doesn't add a trailing space. The same doesn´t happen when using input tap. This helps to conclude that indeed there are differences between these two input methods.
"adb shell input keyevent eventid" for sure will not simulate real keyevent as the device id == 0.
what about "adb shell input text "sometext"" it is anyway not from pysical ... so I guess it will do is as clikcing on softkeyboard ?

How to use ADB to send touch events to device using sendevent command?

I am trying to send touch events to a device using AndroidDebugBridge, so that I can do some basic automation for UI tests. I have followed the discussion in LINK. I am able to use sendevent to simulate touch on emulators, but unable to do the same on a device.
Like in above link the emulator seems to send out 6 events for each touch ( xcoord, ycoord, 2 for press,2 for release) and it was easy to use this information to sendevents, but a getevent for the touchscreen for a device seems to generate far too many events.
Has somebody managed to send touch from ADB to a device? Could you please share the solution.
Android comes with an input command-line tool that can simulate miscellaneous input events. To simulate tapping, it's:
input tap x y
You can use the adb shell ( > 2.3.5) to run the command remotely:
adb shell input tap x y
In order to do a particular action (for example to open the web browser), you need to first figure out where to tap. To do that, you can first run:
adb shell getevent -l
Once you press on the device, at the location that you want, you will see this output:
<...>
/dev/input/event3: EV_KEY BTN_TOUCH DOWN
/dev/input/event3: EV_ABS ABS_MT_POSITION_X 000002f5
/dev/input/event3: EV_ABS ABS_MT_POSITION_Y 0000069e
adb is telling you that a key was pressed (button down) at position 2f5, 69e in hex which is 757 and 1694 in decimal.
If you now want to generate the same event, you can use the input tap command at the same position:
adb shell input tap 757 1694
More info can be found at:
https://source.android.com/devices/input/touch-devices.html
http://source.android.com/devices/input/getevent.html
2.3.5 did not have input tap, just input keyevent and input text
You can use the monkeyrunner for it: (this is a copy of the answer at https://stackoverflow.com/a/18959385/1587329):
You might want to use monkeyrunner like this:
$ monkeyrunner
>>> from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
>>> device = MonkeyRunner.waitForConnection()
>>> device.touch(200, 400, MonkeyDevice.DOWN_AND_UP)
You can also do a drag, start activies etc.
Have a look at the api for MonkeyDevice.
You don't need to use
adb shell getevent -l
command, you just need to enable in Developer Options on the device [Show Touch data] to get X and Y.
Some more information can be found in my article here: https://mobileqablog.wordpress.com/2016/08/20/android-automatic-touchscreen-taps-adb-shell-input-touchscreen-tap/
Building on top of Tomas's answer, this is the best approach of finding the location tap position as an integer I found:
adb shell getevent -l | grep ABS_MT_POSITION --line-buffered | awk '{a = substr($0,54,8); sub(/^0+/, "", a); b = sprintf("0x%s",a); printf("%d\n",strtonum(b))}'
Use adb shell getevent -l to get a list of events, the using grep for ABS_MT_POSITION (gets the line with touch events in hex) and finally use awk to get the relevant hex values, strip them of zeros and convert hex to integer. This continuously prints the x and y coordinates in the terminal only when you press on the device.
You can then use this adb shell command to send the command:
adb shell input tap x y
Consider using Android's uiautomator, with adb shell uiautomator [...] or directly using the .jar that comes with the SDK.

Categories

Resources