There is a way of doing what you want using the adb shell from monkeyrunner and it doesn't require a separate third-party library.
if ('mInputShown=true' in device.shell('dumpsys input_method')):
<conditional code for when soft keyboard is showing goes here>
or
if ('mInputShown=false' in device.shell('dumpsys input_method')):
<conditional code for when soft keyboard is not showning goes here>
where device
is the instance of MonkeyDevice for the attached device.
I've found that applications that usually show a soft keyboard for input upon manual launch don't reliably show one when launched with monkeyrunner. If script logic depends on wehther the soft keyboard is showing or not, I use the above tests in the script to check if the soft keyboard is showing or not.
The following explanation includes some thinking about how to solve problems of this type.
adb shell dumpsys
returns a very large and detailed dump of all services running on the device. The dumpsys
dump may be requested for a single service, in our case, the input services. That usage is
adb shell dumpsys input_method
which will return a much smaller dump which is just the current input method manager state. That dump will include all curent InputMethod instances, input method manager clients with general parameters for input method manager clients, input method client state, and input method server state. One set of general parameters for input method manager clients relates to whether an input method is shown (e.g. soft keyboard) and some parameters about whether the input method show was requested, explicitly requested or forced and whether it is being shown.
Whether the input method is shown is the one of interest since it is true when the soft keyboard is showing and false when the soft keyboard is not showing. That parameter's name is
mInputShown
and will look like
mInputShown=true
or
mInputShown=false
depending on whether the soft keyboard is showing or not.
The next step is making use of this information in a monkeyrunner script. The MonkeyDevice class includes a method for running an ADB shell command from within monkeyrunner's use of the bridge and which returns an object which is the return value to the ADB shell from executing the ADB shell command. Within a monkeyrunner script, that looks like
shell_cmd_return_stuff = device.shell('dumpsys input_method')
where device
is the instance of the MonkeyDevice class for the attached device and shell_cmd_return_stuff
is variable holding all the shell command's output -- in this case the dump output. Finally, since we are looking for a specific parameter/value pair in the text and know what it looks like, we can use standard Jython to look for that string within the output returned without saving it in a variable and using the Jython string in
boolean operator
if ('mInputShown=true' in device.shell('dumpsys input_method')):
<conditional code for when soft keyboard is showing goes here>
or
if ('mInputShown=false' in device.shell('dumpsys input_method')):
<conditional code for when soft keyboard is not showning goes here>
depending on wehther we want to know if the soft keyboard is currently showing or not showing.
Enjoy!