How can I set a listener for long-click performed on hardware Menu button? What is the way of programmatic access to the Menu button?
Moreover how can I distinguish long-click from single-click? (As far as I know when long-click is performed the single-click event is propagated as well - I do not want this to happen because I need 2 different actions for these 2 situations. Long-click and single-click separate listeners for the device Menu button)
Thank you!
This shoule be fairly straight forward. Check out the KeyEvent.Callback on the Android developer's website.
There you will find
onKeyLongPress()
as well asonKeyDown()
andonKeyUp()
. This should get you on the right track. Comment or post you code if you need any further help.Edit: I just re-read the question. If you are having trouble distinguishing single click from long click, you will need to use
onKeyDown
andonKeyUp
and check the duration of the click. Esentially you will start a timer in theonKeyDown
and check the time in theonKeyUp
. You will have to watch forFLAG_CANCELED
.Further Edit: I found the time to do a couple of tests. This code should do what you want (
onKeyUp()
gets only short press events andonLongPress()
gets only long press events).The key thing here is in the call to
event.startTracking()
in theonKeyDown()
handler.Place in
Activity
(this should also work in a custom view as well but untested):