How can I use adb to send a longpress key event?

2019-01-11 08:17发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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.



回答6:

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



标签: android adb