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

2019-01-11 08:13发布

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

标签: android adb
6条回答
Deceive 欺骗
2楼-- · 2019-01-11 08:23

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

查看更多
对你真心纯属浪费
3楼-- · 2019-01-11 08:27

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.

查看更多
劳资没心,怎么记你
4楼-- · 2019-01-11 08:32

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.

查看更多
唯我独甜
5楼-- · 2019-01-11 08:33

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

查看更多
Explosion°爆炸
6楼-- · 2019-01-11 08:34

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.

查看更多
萌系小妹纸
7楼-- · 2019-01-11 08:43

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.

查看更多
登录 后发表回答