Android 如何使用 adb 发送长按按键事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11142843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use adb to send a longpress key event?
提问by whoabackoff
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
谢谢
回答by Nguyen Duc Han
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);
你在屏幕上的申请位置是170、187;延迟时间为2000(ms);
Long press HOME key:
长按HOME键:
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 cmdand type adb shell getevent | find "event2"; long press HOMEkey to see more.
您可以转到cmd并输入adb shell getevent | 找到“事件2”;长按HOME键查看更多。
回答by Nick Chalko
Since this commitin Android 4.4 it is possibile to use:
由于在 Android 4.4 中进行了此提交,因此可以使用:
adb shell input keyevent --longpress KEYCODE_L
This other commitfurther improved the behaviour.
另一个提交进一步改善了行为。
回答by Ovi
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 longpresson 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
您可以无限制地重复事件或输入内容,就像在按键上长按一样。这是同一件事。您现在可以定义自己的长传和时间
回答by avoyles
This linkdiscusses 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.
此链接讨论了一个类似的问题,但有问题的设备(Nexus One 设备)将菜单/主页/返回/搜索按钮作为触摸屏的一部分,而不是物理键。
This other oneappears 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.
另一个似乎更符合注入物理键输入,但需要访问设备驱动程序的 *.kl 文件以确定该特定的设备、类型、键代码、值按下和值释放代码设备。
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.
,其中 n 是按下持续时间的长度(以秒为单位)。
Hopefully this might be of some use.
希望这可能会有一些用处。
回答by Hein
回答by Arpana
This might be too late to answer but surely will help others.
这可能为时已晚,但肯定会帮助其他人。
Please use below cmd to achieve long press.
请使用下面的cmd来实现长按。
adb shell input keyevent 5 sleep 5
adb shell 输入 keyevent 5 sleep 5