'Detecting Long Key Press on Samsung Galaxy Watch 4 (Android Wear OS)
I have written a sailing app for watches running Wear OS. Sailing watches often get wet so I disabled the screen and navigate the menu using physical key presses (single and multiple presses). So far so good
I am now trying to detect a Long Press of the physical key (for an emergency Man-Over-Board function) but so far I have been unable to find any event which is triggered when a physical key is held down on the Samsung Galaxy Watch 4.
Can anyone suggest how to detect a long key press on the Samsung Galaxy Watch 4?
Most of the key press detection can be done by overriding onKeyDown()
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
return if (keyCode == bottomKeyCode) {
// process bottomKeyPress
writeToLog("onKeyDown()")
event.startTracking() // required to enable LongPress (works on TicWatch NOT Samsung)
true
} else
super.onKeyDown(keyCode, event)
}
On the Samsung Galaxy Watch 4 a short press will trigger the onKeyDown() event
The problem is no events are triggered when the key is held down. Holding down the key does not trigger onKeyDown or onKeyLongPress. It does not even trigger onKeyUp when the key is released!
Further testing
I have also looked at dispatchKeyEvent()
override fun dispatchKeyEvent(event: KeyEvent): Boolean {
writeToLog("dispatchKeyEvent() keyCode ${event.keyCode} keyAction ${event.action}")
return super.dispatchKeyEvent(event)
}
This was also unsuccessful
I have run the code on a Ticwatch Pro 3 GPS (Wear OS 2) and the behaviour is 'closer' to what is described in the Android documentation. When the key is first pressed onKeyDown() is triggered. Continuing to hold the key down results in a second onKeyDown() 350msec later. This is followed by more onKeyDown() events sent every 50msec after that (along with an onLongKeyPress()). Hence the easiest way to implement Long Key Press detection on the TicWatch is to simply count the number of onKeyDown() events (to avoid the unneeded onLongKeyPress() event simply remove event.startTracking()).
Note the Samsung Galaxy Watch 4 uses keyCode == KeyEvent.KEYCODE_BACK for the bottom physical key rather than the Ticwatch which uses KeyEvent.KEYCODE_STEM_1. For completeness I investigated onBackPressed() but this is also not being triggered
override fun onBackPressed() {
writeToLog("onBackPressed()")
super.onBackPressed()
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
