CapitalSpace
CapitalSpace

Reputation: 3

(Tizen studio, wearable, web app) How to detect long press on back-hardware button?

I'm developing a web app for Samsung wearable device and I would like to detect when user makes a long press on a back-hardware button.

The only information that I have found so far is this:

document.addEventListener('tizenhwkey', function(e) {...});

The problem? This event fires only when back-button is released, but unfortunately it doesn't fire when the back-button is pressed - and for detecting long press when need information when back-button is pressed and when it was released.

Any ideas how to detect a long press of back-hardware button?

Upvotes: 0

Views: 434

Answers (2)

Loebstahhhh
Loebstahhhh

Reputation: 164

There indeed is no way to detect a long press with Tizen Web. An alternative could be to detect a quick double-press, instead of a long press. That can be accomplished using timers.

E.g.

const doubleClickWaitTime = 400;
let doubleClickTimeout = -1;

window.addEventListener('tizenhwkey', function(ev) {
    if (ev.keyName === 'back') {
      if (doubleClickTimeout === -1) {
        // First click of back button
        doubleClickTimeout = setTimeout(() => {
          doubleClickTimeout = -1;
          backButtonSinglePress();
        }, doubleClickWaitTime);
      } else {
        // Second click of back button
        clearTimeout(doubleClickTimeout);
        doubleClickTimeout = -1;
        backButtonDoublePress();
      }
    }
});

function backButtonSinglePress() {
  // Single press
}

function backButtonDoublePress() {
  // Double press
}

This does the following:

  • When the user clicks the hardware key, a timer is started that fires a function after doubleClickWaitTime milliseconds.
  • If the user presses the hardware button again during this time, the timer is canceled, and backButtonDoublePress() is called.
  • If the user doesn't press the hardware button again, the timer fires and executes backButtonSinglePress() after resetting the timer.

The doubleClickWaitTime variable can be modified to give the user more time for a double click. However, the longer this delay, the longer a single click event takes to fire.

Upvotes: 1

SangHyup Lee
SangHyup Lee

Reputation: 1

I checked the docuement. But Tizen platform does not provide a way to distinguish between press and release of hardware key.

https://docs.tizen.org/application/web/guides/tau/event-handling/

Upvotes: 0

Related Questions