RatherBeSailing
RatherBeSailing

Reputation: 311

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()
    }

Upvotes: 5

Views: 745

Answers (1)

RatherBeSailing
RatherBeSailing

Reputation: 311

Samsung finally responded and the formal answer is Long Press is not supported

Unfortunately, it is not possible to get dispatched long-press keyEvent from the Samsung Galaxy watch 4.

Both Hardware buttons (named Home key and Back key) of watch 4 are system keys.

According to Samsung's policy, in the case of system keys, 3rd party can not get the KeyEvent of the watch 4 devices.

Factually it is possible for 3rd party apps to get access to the onKeyDown() event (as shown in the original question) PROVIDED it is only a short press

If the back key is held down then the watch does not trigger the onKeyDown() event (meaning any attempt to use a timer to simulate an onLongKeyPress() event will also be unsuccessful)

Upvotes: 4

Related Questions