Search code examples
androideventscommandadbkeyevent

How to send a adb event for long press a key


I am trying to do some auto testing and I need to trigger a long press of a key using adb command from my PC. For example, a long press home button would bring up the lates used apps. I found that key event #3 is for home key. But I could not figure out how to trigger a long press of the home button. Some post here suggest using sleep command. But it does not work for me, it always trigger the single click event and then go to sleep. It is not doing a long press action. Please help.

What I tried is: adb shell input keyevent 3 adb shell sleep 2

I am just using home key as an example. It can be any key on an Android device, for example, the dpad key. back key, etc.


Solution

  • sleep did not work for you because the input keyevent command sends both key down and key up event before it completes. To properly emulate the "long" press with sleep you need to send separate event for down and up and use sleep in between. There are plenty of answered questions about sendevent usage here on SO.