How To Send Key Down And Key Up Events Separately On Android Using Adb?
Solution 1:
I've done this long time ago, so I don't remember all the details. Hope I don't miss anything crucial...
All the input hardware that exist on the device are mapped into files (it's Linux, so of course everything is a file...). If you do adb shell ls /dev/input
you see few eventX
files, from event0
and up. These files are mapped to the screen (the touch screen, not the display! which is usually event2
), hard keys (usually event0
), proximity sensor and all the other sensors/inputs of the device.
Another thing to bare in mind is these are not "normal" files, but "character files" - it's kind of a buffer that has data only at the moment that the relevant input device is activated. You can type adb shell cat /dev/input/event0
and you won't see a thing until the moment you press some physical key on your device, like home
or vol up
. When you press a key, you see some gibrish on your screen, but that gibrish has meaning of course.
Type adb pull /dev/input/event0
, press and release the vol up
and press ctrl-c
to stop the pull
command. Now you'll have a binary file called event0
, so open it with some Hex editor and you'll get something like -
59 07 00 00 80 C9 07 00 01 00 18 00 01 00 00 00 59 07 00 00 80 C9 07 00 00 00 00 00 00 00 00 00 5A 07 00 00 96 60 01 00 01 00 18 00 00 00 00 00 5A 07 00 00 96 60 01 00 00 00 00 00 00 00 00 00
The first 8 bytes of each row are timestamp of the event and the other 8 bytes are the event itself.
The first row is pressing the vol. up
key (little-endian):
01 00
is the ID of the event. If you press two keys simultaneously you get two events with two IDs.
18 00
is the key code.
01 00
is the event type. 01
is press, 00
(like line 3) is release.
00 00
(the last two bytes) are not in use here. For screen touch for example, it holds the screen coordinates.
The second row is some buffer cleaning/sync event that contains all zeros.
The third row is releasing the key. It's the same as the first row, but the event type is 00
.
The fourth row is again the sync event.
Now, if you create a file called eventFile
that contains the first two rows, you actually have a sequence of pressing the vol. up
key. You can inject it to the device with - adb push eventFile /dev/input/event0
and you'll see that the vol. up
key is pressed but not released - a long press. To release it, just send in another file with the other two lines. You don't have to worry about the time stamp, you can leave it as-is.
For different keys, you'll have to change the key code
of course.
I've tried it on my device - Samsung S3 mini /4.1.2 with both vol up/down
keys and it works. I didn't try it with non-existing keypad keys.
Solution 2:
To help anyone who might need this information later here's what I've pulled together from @TDG post and other StackOverflow answers.
There are different files available that map to device hardware, such as the touch screens or physical buttons. It's not clear if the same files map to the same hardware on all Android phones, but I would expect there are some devices out there that differ so be careful.
These files are called /dev/input/eventX, where X refers to a 0-indexed number for the hardware.
To find the correct file for the device you are interested in you can use getevent
.
adb shell su 0 getevent
This will output the name and ID of each device. When you have the correct path you can start listening for incoming events:
adb shell su 0 getevent /dev/input/event0
When that's running trigger the event on your device, for instance press the button you're interested in. This will output something like:
0001 0095 000000010000 0000 000000000001 0095 000000000000 0000 00000000
These were the entries when I pressed and released a hardware button. Note that there are 2 sets for each event (first 2 are key down, second 2 are key up)
Note the values are in hex. Convert them to decimal. Use a calculator or hex2dec site to do this. You can then inject the events using sendevent
Key down
adb shell su 0 sendevent /dev/input/event0 1 149 1 # Hex: 0001 0095 00000001
adb shell su 0 sendevent /dev/input/event0 0 0 0 # Hex: 0000 0000 00000000
Key up
adb shell su 0 sendevent /dev/input/event0 1 149 0 # Hex: 0001 0095 00000000
adb shell su 0 sendevent /dev/input/event0 0 0 0 # Hex: 0000 0000 00000000
Post a Comment for "How To Send Key Down And Key Up Events Separately On Android Using Adb?"