adb send keystroke doesn't work - android

I'm trying to emulate input from my android application to make some test.
Well, my Android phone (AREO 8.1.0) is connected to USB and my debugger is enabled.
I use some
adb shell input tap 90 310
adb shell input text "banana"
and my keyboard open as well with "banana" text in the text field (keyboard and apps textfield)
The problem is the next step:
adb shell input keyevent 66 # ENTER input
It won't send this command.
I'm thinking its about my custom keyboard.
I use
to find whats the keyevent I need.
I also use the
keyevent KEYCODE_ENTER
keyevent KEYCODE_TAB
...
the keyevent BACK, SPACE, CLEAR works as well but not the ENTER
*by the way, I tried with and without --longpress
Anyone know what is the problem?

Related

Is it possible to bring up Android keyboard via adb?

Is it possible to bring up the Android soft-keyboard by sending some kind of adb command?
To show the keyboard:
adb shell input keyevent 108
to hide it:
adb shell input keyevent 111
But this only works if you're already focused on a view that is ready to receive text (like EditText).

How to bring app to Foreground in calabash-android?

I am working on a application, it contain social share options. Whenever I tap on facebook it will open facebook app in the device with share view.
After that, I want to bring my application to foreground.
I tried pressing back button,
press_back_button()
it is unsuccessful.
Is there any way to bring application to foreground in Calabash-android?
You should be able to do it by sending keypresses and screen touches with ADB
adb shell input keyevent KEYCODE_APP_SWITCH to open the recent apps menu
adb shell input tap 300 300
Will send a tap event on the screen but you'll have to play around with the coordinates to get them in the right place.
EDIT:
I just tried on my phone with the following commands and can reopen the most recent app
adb shell input keyevent KEYCODE_APP_SWITCH
adb shell input tap 300 1700
Before trying these commands on your ruby code see if they work from the terminal (not irb).
The second command uses x and y coordinates that will be different depending on the size of the screen on your phone, 300 1700 works for me but you will need to try some different numbers to make it work for you.
To open the second most recent app, which I think is what you will need, I have to run adb shell input tap 300 700. You will need to play around with this and see what works for you.
FINAL EDIT: I've found a nicer way to do this that's not dependant on screen resolution.
adb shell input keyevent KEYCODE_APP_SWITCH
Then to find your app (you may need to send these multiple times to get it to work)
adb shell input keyevent KEYCODE_DPAD_UP
and/or
adb shell input keyevent KEYCODE_DPAD_DOWN
Then to select
adb shell input keyevent KEYCODE_ENTER
you need to run the system command, e.g.
system("adb shell input keyevent KEYCODE_APP_SWITCH”)
This willl open recent activities.

Android ADB: Simulate Recent App key press

Is there a way to get the use ADB to simulate a Recent App key press? I do not see it in the list of assigned keys and I am not able to select it using adb input shell tap despite getting the coordinates from the debugging options.
I am trying to automate a task on my own personal phone (Nexus 5 with soft keys) so any hacky way is fine, assuming there is not a clean way to do this.
The solution is to use the KEYCODE_APP_SWITCH KeyEvent:
adb shell input keyevent KEYCODE_APP_SWITCH
See #SimonMarquis answer below
adb shell input keyevent KEYCODE_APP_SWITCH
(OBSOLETE)
I don't think there is a keycode for it.
However, I am able to open the recent apps menu with adb shell input tap (testing on a Nexus 5 with Lollipop). For example:
adb shell input tap 800 1890
adb shell input keyevent KEYCODE_APP_SWITCH
Still works, you don't even have to put its numeric value (187) into the 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 ?

Adb shell commands to change settings or perform tasks on a phone

How do I use adb to perform some automated tasks on my android phone? I need to find commands that I can issue from the command line (ideally, using a .bat file) that will be capable of more than simply opening an application or sending an input keyevent (button press).
For instance, I want to toggle Airplane Mode on or off from the command line. Currently, the best I can do is launch the Wireless & network settings menu and then use input keyevents to click Airplane mode:
adb shell am start -a android.settings.AIRPLANE_MODE_SETTINGS
adb shell input keyevent 19 & adb shell input keyevent 23
There are quite a few drawbacks to this method, primarily that the screen has to be on and unlocked. Also, the tasks I want to do are much broader than this simple example. Other things I'd like to do if possible:
1.Play an mp3 and set it on repeat. Current solution:
adb shell am start -n com.android.music/.MusicBrowserActivity
adb shell input keyevent 84
adb shell input keyevent 56 & adb shell input keyevent 66 & adb shell input keyevent 67 & adb shell input keyevent 19
adb shell input keyevent 23 & adb shell input keyevent 21
adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 19 & adb shell input keyevent 22 & adb shell input keyevent 22 & adb shell input keyevent 23 & adb shell input keyevent 23
2.Play a video. (current solution: open MediaGallery, send keyevents, similar to above)
3.Change the volume (current solution: send volume-up button keyevents)
4.Change the display timeout (current solution: open sound & display settings, send keyevents)
As before, these all require the screen to be on and unlocked. The other major drawback to using keyevents is if the UI of the application is changed, the keyevents will no longer perform the correct function. If there is no easier way to do these sort of things, is there at least a way to turn the screen on (using adb) when it is off? Or to have keyevents still work when the screen is off?
I'm not very familiar with java. That said, I've seen code like the following (source: http://yenliangl.blogspot.com/2009/12/toggle-airplane-mode.html) to change a setting on the phone:
Settings.System.putInt(Settings.System.AIRPLANE_MODE_ON, 1 /* 1 or 0 */);
How do I translate something like the above into an adb shell command? Is this possible, or the wrong way to think about it?
I can provide more details if needed. Thanks!
Although question is old, it might help someone else.
For video playback, you can try this:
adb shell am start -a android.intent.action.VIEW -d "file:///mnt/sdcard/DCIM/Camera/test.3gp" -t "video/*"
^gives you a prompt of all capable players that can play this file.
adb shell am start -a android.intent.action.VIEW -d "file:///mnt/sdcard/DCIM/Camera/test.3gp" -t "video/*" -n "com.alensw.PicFolder/.PlayerActivity"
^plays in player specified by switch -n.
I'm working on the same set of issues. (I mostly solved the context issue with straight button presses by using the keyevent HOME and then MENU, but -- somehow -- even that's unreliable.) I'm currently investigating SL4A (Scripting Layer for Android), which has promise. It allows Perl, Python, Lua, and other scripting languages to interact with the Android API from your PC after starting an SL4A server on the device -- which can also be done from the PC. I'm finding "Pro Android Python with SL4A" to be an excellent resource; I would have saved myself days of trial-and-error and hunting on the Web if I had started with that book
The Java example you show is for a program that is running on the phone itself. You might be able to program some kind of an interpreter on the phone that handles adb commands. That way you are not dependent anymore on keyevents. This is not a minor undertaking, however.
MonkeyRunner also looks like it has promise, though I haven't dug deep enough to find its limits yet.
Android ScreenCast lets you view and control your device's screen from your PC, which also has potential for automation. It does have present some logistical issues for that application, though.
By the way, you can get past the locked homescreen with a MENU keyevent and a set of sendevents (in place of keyevents).

Categories

Resources