9

I can input the text into a dialog with adb shell input text "blahblah" just fine. The text shows up and... sits there.

I can send a tap at coords of the OK button, to actually have the text take effect in whatever dialog I entered it - if I guess the screen orientation right, adapt to the current resolution, adapt to the current keyboard variant and so on. Terribly clunky.

Is there some magic character or keycode or some other neat way to have the shell perform the equivalent of pressing the "OK"?

example

In the above screenshot, it's the green ->| icon in the lower right. It sometimes changes with the exact field used, but the meaning is always the same: close the keyboard and proceed.

SF.
  • 13,549
  • 14
  • 71
  • 107
  • Which OK button? Is it an app that you wrote, or something you've downloaded? – TDG Jun 10 '16 at 19:50
  • 1
    @TDG: The standard confirm button present on the virtual keyboard provided by the OS. It appears in all apps, and in system configuration too, whenever you face text entry. Depending on context the icon may change - lens if you're searching, `->|` if you're entering some data, [OK] in the password field, but the meaning is identical, finish text entry and proceed. – SF. Jun 10 '16 at 20:16
  • (also, the location is identical, lower right of the keyboard) – SF. Jun 10 '16 at 20:29
  • I have a similar unanswered question at: https://stackoverflow.com/q/39906970/2291928 . Unfortunately it seems like app devs need to build in Enter Key (keycode 66) listener support for the text field because ADB, and therefore Appium or anything else, cannot hit OK, Done, Continue, Search, or any of the `IME_ACTION`s. – George Pantazes Aug 07 '17 at 20:41
  • To solve that , I send key_event TAB (61) then enter (66) see my solution : https://stackoverflow.com/a/68170667/2267723 – Maher Abuthraa Jun 28 '21 at 22:32

3 Answers3

14

I believe that you're looking for adb shell input keyevent 66.
The keyevent is using for pressing the virtual keyboard keys, and code 66 is for ENTER key.
You can find here list of codes.

EDIT The mapping between the keys and the codes can be found at /system/usr/keylayout/qwerty.kl. You can do adb shell cat /system/usr/keylayout/qwerty.kl and see the codes you need.

TDG
  • 5,909
  • 3
  • 30
  • 51
  • That's all fine and dandy for single-line inputs, but it won't work for inputs where "newline" is a legal input (say, SMS entry). These have a separate "enter" and "Confirm" buttons. – SF. Jun 11 '16 at 08:24
  • Then you have to find the right code for each button. – TDG Jun 11 '16 at 08:51
  • Okay. How do I do this? – SF. Jun 11 '16 at 10:53
  • Still no such key. It seems the button isn't mapped to any keycode, but instead broadcasts IME_ACTION_DONE intent. I'm currently messing with the `am` command to try to simulate this. – SF. Jun 11 '16 at 18:22
2

You can use AndroidViewClient/culebra and forget about orientation, different screen sizes, etc.

As an example, let's say we want to enter text and press OK in this dialog (part of Api Demos)

enter image description here

just run

culebra -uGo myscript.py

when the window is displayed, click on the entry, type the text, then click OK and this script will be automatically generated

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2016  Diego Torres Milano
Created on 2016-06-10 by Culebra v11.5.8
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os


try:
    sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient

TAG = 'CULEBRA'

_s = 5
_v = '--verbose' in sys.argv


kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
vc = ViewClient(device, serialno, **kwargs2)
#vc.dump(window='-1') # FIXME: seems not needed

vc.dump(window=-1)
vc.findViewByIdOrRaise("com.example.android.apis:id/username_edit").setText(u"hello culebra!")
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'OK').touch()

when run, it will set the text to hello culebra! and touch OK. You can then adapt the generated script to your needs.

CulebraTester

CulebraTester is a new implementation of culebra as a service that runs on the device under test. It's now under closed beta testing but if you are interested in participating you can find the opt-in form at culebra.dtmilano.com (check under Support).

One of its features is to be able to detect the virtual keyboard and treat it the same as other Views.

enter image description here

This screenshot illustrates the test generated after touching A, b, c, and Next (which is what your are looking for).

The generated test is like this

/**
 * @@Test comment here@@
 *
 * @throws Exception
 */
@Test
public void culebraGeneratedTest() throws Exception {
    mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("A").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
    mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("b").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
    mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("c").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
    mDevice.findObject(By.clazz(Pattern.compile(".*")).desc("Next").pkg("com.android.inputmethod.latin")).clickAndWait(Until.newWindow(), DEFAULT_TIMEOUT);
}

and you can compile, install and run as any other UiAutomator test.

As an additional example, this screenshot show how as you hover the virtual keyboard Views in the tree they are highlighted in the device representation

enter image description here

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • 2
    That's not the OK on the virtual keyboard. Please see my clarification edit. – SF. Jun 10 '16 at 20:23
  • 1
    I undersntand. AndroidViewClient relies on `uiautomator dump` to obtains the Views on the screen and it doesn't include the keyboard. However, [CulebraTester](http://culebra.dtmilano.com/) provides a different approach and the keyboard is accessible. CulebraTester is on private beta now and requires an app to be installed on the device. There's an **opt-in form** to participate in the program at the end of that page. – Diego Torres Milano Jun 10 '16 at 20:53
  • I'll wait if someone provides some simpler method. For example, I've seen people jumping through hoops to enter space bar in the text (plain ' ' wouldn't work), then someone used just `%s` within the string, which would be substituted for space by the `input` command.. Your script would certainly work but it's an awfully roundabout way of doing what *should* be achievable with a single shell command... – SF. Jun 10 '16 at 21:07
  • Perhaps your approach is wrong and you should not be relaying on the virtual keyboard at all. You can type, set text and touch any UI element why do you need it. And escaping the space and other ton of things are handled by AndroidViewClient automatically. – Diego Torres Milano Jun 10 '16 at 23:47
  • In the specific context where I want to use it, there's little wiggle room: Unlock screen, without disabling the screen security. There's a simple "input swipe" that works on a screen set to unlock on swipe, but that's unlocked for all practical purposes, security disabled. There's no simple way to perform a complex swipe to unlock a pattern lock. I don't want to use PIN, which would work, but I really don't want to risk locking my SIM card. But I can enter the password if I protect the screen with a password, I just need to finish the password entry with "OK". – SF. Jun 11 '16 at 01:05
  • Well, there's a way of performing a swipe that allows you to unlock the device via a pattern using **CulebraTester**, see **swipe** https://github.com/dtmilano/CulebraTester-public/wiki/RESTful-API#swipe – Diego Torres Milano Jun 13 '16 at 20:19
-1

The neatest way is to make use of UiAutomator, But below method is simple, will this help ? If your device is same, try - Android Record N Play. This will record screen and you can play later. Also saves a shell script for your actions.If you want to play(ui actions) independently without the tool, record first using the tool and just call adb shell sh script_name to playback.

Rilwan
  • 2,251
  • 2
  • 19
  • 28