Premise:
It is often necessary to simulate the physical keys in our application or when we are writing Android's own active test, to give us a test. This is what I want to tidy up.
The basic concerns are the Linux Input event events.
The following sample is based on Android 5.1 (Linux-3.10).
First, the use of GetEvent, sendevent analog buttons
(1) command: Getevent-t
Results:
...Add Device6:/dev/input/event9 bus:0000Vendor0000Product0000Version0000Name"Msm8994-snd-card button Jack."Location"ALSA"Id:""Version1.0. 1Events:key (0001):0100 0101 0102 0103 0104 0105 0106 0107Input props: <none>...
Know: The corresponding input event for the headset key we want is event9.
(2) command: Getevent-t/dev/input/event9 (getevent-l/dev/input/event9 will have more specific information), use press the headset button to pre-obtain headset event information
Results:
按键按下:0001 00e2 000000010000 0000 00000000松下按键:0001 00e2 000000000000 0000 00000000
(3) using the sendevent command to simulate triggering a line-control key event
sendevent /dev/input/event9 0001 00e2 00000001sendevent /dev/input/event9 0000 0000 00000000**这个之间要延时500ms~1s,不然会被系统误觉得是误触发的**sendevent /dev/input/event9 0001 00e2 00000000sendevent /dev/input/event9 0000 0000 00000000
Note: only input events can be simulated with sendevent. But sometimes it's too much of an analog touch-screen slide, assuming that sendevent requires a lot of command to simulate.
This can be replaced with the input command.
Second, using the input command to achieve analog keys, touch screen operation
(1) Analog keys
command: Input keyevent {key value}
The key value of the known Android phone volume + (KEYCODE_VOLUME_UP) is 24, which uses
Input KeyEvent 24 simulates pressing the volume + key.
Many other key values are shown in annex 1.
(2) Analog touch screen operation (Tap, swipe)
command: Input swipe {start point x axis value} {Start point Y axis value} {endpoint X axis value} {end point Y axis value} {sliding time [ignorable]}. Time Unit: Ms
For example: from x=100,y=500. Slide to x=600,y=500. Time 100ms
Input swipe 100 500 600 500 100
command: Input tap {x-axis value} {y-axis value}
For example: Click x=100,y=500 this point. Simulate clicking the app icon to open the app
Input TAP 100 500
Annex 1
Android (Linux) analog keys, touch screen and other events