onKeyPressed() and onKeyReleased() works on Sim but doesn't work on VivoactiveHR

Former Member
Former Member
Hi all,

I'm trying to do a widget that when a Key is pressed for at least 3 seconds, when it is released the widget will do certain action.
Everythings works on Simulator, but when I try on real target (Vivosmart HR) it does not work.
I look in the log in the Vivosmart HR and I do not see the message KEY PRESSED.
Instead doing onHold on the screen of Vivoactive HR it works both on Simulator and on Vivoactive HR
I report the code of the function KeyPressed and KeyReleased and onHold:

function onKeyPressed(evt)
{
keyPressedTime = System.getTimer();
Sys.println("KEY PRESSED\n");
return true;
}

function onKeyReleased(evt)
{
if (keyPressedTime > 0)
{
var delta = System.getTimer() - keyPressedTime;// ms since last press
keyPressedTime = 0;
if (delta > 3000)
{
Sys.println("LONG KEY RELEASED\n");// We have a hold

}
else
{
Sys.println("NORMAL KEY RELEASED\n");// We have a regular press
}
return true;
}
return false;
}


function onHold(evt)
{
Sys.println("HOLD\n");
return true;
}

Thanks for help me!!
  • onKeyPressed and onKeyReleased is tricky and works differently based on the specific device. In this case, the vahr does different things based on a press or long press of the two buttons. search the forum, as there are a number of posts about onKeyPressed/onKeyReleased

    With the right button, a short press of right is key_start, and a long press is key_menu, so you can just use that to check for a long press. With the left button, short is key_esc, long, takes you to things like poweroff (and you can't catch it). On other devices, a long press can be tied to a hot key, and on the 645m for example, a long press of down takes you to the music controls. On many non-touch watches, long press of up is "menu".

    for onHold, there is a difference in that it's not a key and only for touch screens. But even there, on a va3, a long press of the screen is "menu".

    So a simple answer is on the vahr, to tell the difference between a short and long press of the right button, look for key_start vs key_menu (or onMenu())
  • Former Member
    Former Member over 7 years ago
    Hi jim thanks for your explanaition...So there is no way to detect a long pression (more than 3 seconds)? becuase the long press that became menu I don't know how is long?
    Have you any suggestion to how detect a long pression (at least 3 seconds) of some key s?

    Thanks
  • What I'd do, is rethink the UI, and try to go by what's in the UX guide and how things work in a native app. Anything like detecting an "extra long press" could be a headache when you start supporting other devices, and also it's something that might not be obvious to a user.

    If you're looking at trying to change something in your app, you can look at things like using a menu or picker to make changes.