I can use something like:
adb shell input keyevent 4
and this will send a single 'Back' button press to my device. How can I send a longpress?
Thanks
You can try this command:
adb shell input touchscreen swipe 170 187 170 187 2000
Your application position on screen is
170, 187; delay time is 2000 (ms);
Long press HOME key:
adb shell sendevent /dev/input/event2 1 172 1
adb shell sendevent /dev/input/event2 0 0 0
timeout 1
adb shell sendevent /dev/input/event2 1 172 0
adb shell sendevent /dev/input/event2 0 0 0
You can goto cmd and type adb shell getevent | find "event2" ; long press HOME key to see more.
Since this commit in Android 4.4 it is possibile to use:
adb shell input keyevent --longpress KEYCODE_L
This other commit further improved the behaviour.
When you want to delete something or repeat some Event or just input a lot of numbers, you can use code like the following. It will imitate a longpress on a keyboard:
adb shell input keyevent KEYCODE_FORWARD_DEL KEYCODE_FORWARD_DEL KEYCODE_FORWARD_DEL //delete 3 times
adb shell input keyevent KEYCODE_1 KEYCODE_1 KEYCODE_1 //input value '111'
You can repeat the event or input things without limits, just like a Longpress on the key. It's the same thing. You can define your own longpass and times Now
This link discusses a similar problem, but the device in question (a Nexus One device)has the menu/home/back/search buttons as part of the touchscreen, not physical keys.
This other one appears to be more inline with injecting a physical key input, but requires accessing the *.kl file for your devices driver to determine the device, type, key-code, value-press, and value-release codes for that specific device.
However, the common link between the two appears to be
adb shell sleep n
, where n is the length (in seconds) of the press duration.
Hopefully this might be of some use.
Well, this developer link show the keycode is 128, which i already test, but no expected result
You can check this link and this link. They show how to find it.
This might be too late to answer but surely will help others.
Please use below cmd to achieve long press.
adb shell input keyevent 5 sleep 5
Related
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.
I know I can unlock the screen, pull down the notifications, and press the clear notifications button, but there's got to be a way to clear the notifications through ADB, right? I'm guessing it's some Intent sent through the 'am' command, or maybe something even simpler, but I can't seem to find anything on the net. All I'm getting is Java code for use with an apk.
Edit: I should probably mention that I'm running on 4.3, sometimes commands may vary between versions.
Try:
adb shell service call notification 1
If you know the type of the device and Android version, you can clear notifications using ADB without having rooted device.
The idea is to pull down notifications and swipe away all notifications one by one.
Pull down:
adb shell input swipe 0 0 0 300
Swipe away:
adb shell input swipe 0 400 300 400
It is important to mention, that the (x,y) is something that vary between different type of devices and Android versions. You will need to find by several checks what is the best x,y for you.
The full script
adb shell input swipe 0 0 0 300
num=$(adb shell dumpsys notification | grep NotificationRecord | wc -l)
echo $num
while [ $num -gt 0 ]; do
adb shell input swipe 0 400 300 400
num=$(( $num - 1 ))
done
More details can be found here: https://www.sromku.com/blog/android-adb-clear-notifications
I'm unsure if the question about injecting events via adb is supposed to be in StackOverflow or Android Enthusiasts, please move it if it doesn't belong here.
Anyway, my question is as follows.
I obviously have to determine the "type" of device for sending and receiving events. I can't obviously send a touch event to the keypad device.
After a lot of research I found the sendevent and getevent commands.
So, I want to send a long press to the power button of a phone.
I use this currently:
sendevent /dev/input/event3 1 116 0
sendevent /dev/input/event3 1 116 1
This works on the HTC Wildfire(click on the link for the input devices) because the keypad contains the power button and 116 happens to be the scan code for the power key.
I know what /dev/input/event3/ and 116 and 0 or 1 stand for. What does the 1 in-between /dev/input/event3/ and 116 stand for? How do I obtain it?
Moving on to the Nexus 4. Now, I've noticed that it has a separate powerkey and keypad handler
[EDIT]
Found this regarding sendevent and getevent on XDA.
The 1 "in-between /dev/input/event3/ and 116" stands for EV_KEY event type constant:
EV_KEY:
Used to describe state changes of keyboards, buttons, or other key-like
devices.
You could have found that on your own if you had run getevent -l /dev/input/event3/ and pressed the power key.
Also to find out the power key input device name I would recommend parsing output of getevent -pl instead of contents of /proc/bus/input/devices. The device you are looking for has KEY_POWER listed in the events section:
add device X: /dev/input/eventX
name: "xxxxxxxxxx"
events:
KEY (0001): KEY_POWER
And the proper long power key press sequence (as in press and hold for 1 second and then release) would be:
sendevent /dev/input/eventX 1 116 1
sendevent /dev/input/eventX 0 0 0
sleep 1
sendevent /dev/input/eventX 1 116 0
sendevent /dev/input/eventX 0 0 0
Note: getevent -pl is not available for Gingerbread and below.
Referencing this thread:
http://groups.google.com/group/android-beginners/browse_thread/thread/8a5d8fa9229114d2/ce6e604f52b5318f?pli=1
I know following will send a touch event (5,29) on the device.
adb shell sendevent /dev/input/event0 3 0 5
adb shell sendevent /dev/input/event0 3 1 29
adb shell sendevent /dev/input/event0 1 330 1
adb shell sendevent /dev/input/event0 0 0 0
adb shell sendevent /dev/input/event0 1 330 0
adb shell sendevent /dev/input/event0 0 0 0
However, trying on the real device, it doesn't work. (Tried Nexus S, HTC G2 rooted)
I used
cat /proc/bus/input/devices
or
getevent
to find out which event# is the touch events and send the above code, but no luck.
(Actually I tried all event#s, but none of them work)
How do I send touch events using ADB on real devices?
For key events, I know there's:
input keyevent <event_code>
Is there such one for touch events?
I know I can record/playback touch events. However, I am asking for programmatically sending touch events.
See the (slightly adapted) 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(5, 29, MonkeyDevice.DOWN_AND_UP)
You can also do a drag, start activies etc.
Have a look at the api for MonkeyDevice.
Use dispatchTouchEvent (MotionEvent event) method of View class to send touch (down,move,up) events
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.