onKeyPressed vs OnKey

I'm trying to utilise the ENTER key to recognise 2 different types of input based on whether the user HOLDS the physical key for a longer time vs a single press.

I tried onKeyPressed(), which works, but it also triggers onKEY() at the same time..

is this expected behaviour or am I supposed to do something in the background by putting a timer and making sure the user presses the key for x seconds before triggering a flag or call a callback method?

onKeyPressed keynum:0
onkey :4
onKeyPressed keynum:0
onkey :4


function onKeyPressed(evt) {
var keynum = Lang.format("$1$", [evt.getType()]);
Sys.println("onKeyPressed keynum:" + keynum);
return true;
}


function onKey(evt) {
var keynum = Lang.format("$1$", [evt.getKey()]);
Sys.println("OnKey :" + keynum);
}
  • If you want to handle key holds I think you should probably use onKeyReleased in combination with onKeyPressed, and you probably want to ignore the onKey events entirely.
  • most likely I'm doing it wrong, but my few tries wasn't successful in handling it
  • This seems to work for me in the simulator.

    hidden var keys;

    function initialize() {
    InputDelegate.initialize();
    keys = new [26];
    }

    function onKeyPressed(evt) {
    var key = evt.getKey();

    //if (keys[key] == null) {
    var now = Sys.getTimer();
    keys[key] = now;
    //}

    return true;
    }

    function onKeyReleased(evt) {
    var key = evt.getKey();

    // this can happen if view/delegate is popped as the result of an onKey
    // or onKeyPressed event.
    if (keys[key] != null) {
    var now = Sys.getTimer();
    var delta = now - keys[key];

    Sys.println(Lang.format("Key $1$ held for $2$ms", [ key, delta ]));

    keys[key] = null;
    }

    return true;
    }


    Travis
  • Thanks Travis.. I was looking at it more and I also can get it to work on the simulator as well.

    function onKeyReleased(evt) {
    var keyPressDuration = Sys.getTimer() - keyPressStart;
    var keynum = Lang.format("$1$", [evt.getKey()]);
    var keynum2 = Lang.format("$1$", [evt.getType()]);
    Sys.println("AA :" + keynum + " BB:" + keynum2);
    if ( keyPressDuration >= 2000 ) {
    Sys.println("11 onKeyReleased :" + keynum + " keyPressDuration:" + keyPressDuration + " lockScreen:" + lockScreen);
    } else {
    Sys.println("22 onKeyReleased :" + keynum + " keyPressDuration:" + keyPressDuration + " lockScreen:" + lockScreen);
    }
    }
    return true;
    }


    but again - it's a simulator :p

    Edit : I removed any references to onKey and only used onKeyPressed() and onKeyReleased()
  • Getting reports that it does;t work on an actual device tho....
  • i _think_ I may have located the issue.

    The onKeyReleased is being captured while the onKeyPressed is not.

    eg:
    function onKeyPressed() { aa = sys.getTimerl();}
    function onKeyReleased() {bb = sys.getTimer - aa)


    when you are on the APP list, you do a ENTER (to select the app). You then go into the app, and then 2nd thing you do is you release the ENTER key. (this calls onKeyReleased() function). But since onKeyPressed is not called, aa is either NULL or 0 (i initialised it as zero).

    What happens then is onKeyReleased(), bb becomes a HUGE number (180000023323 - 0) and then, in my case, the lockscreen flag gets triggered.
    Workaround this was to check for aa = 0 or rather if ( aa!=0) ( bb = sys.getTimer - aa)

    Seems to work - the logic anyways - on my F3.
  • You then go into the app, and then 2nd thing you do is you release the ENTER key. (this calls onKeyReleased() function). But since onKeyPressed is not called, aa is either NULL or 0 (i initialised it as zero).


    That is exactly the type of thing I'm guarding against above...

    // this can happen if view/delegate is popped as the result of an onKey
    // or onKeyPressed event.
    if (keys[key] != null) {
    var now = Sys.getTimer();
    var delta = now - keys[key];

    keys[key] = null;
    }


    I hadn't considered the key presses that might lead to your view being pushed, but it is the same problem. I'm using null as an indicator we have processed onKeyPressed() for this onKeyReleased().

    Travis
  • I also found out that need to re-initialise the aa back to 0 once it's done processing the action. Else, going into a Menu option (eg: Save/discard/resume) will still be counted as part of the "duration"

    It seems to work fine on my F3 (but I need to disable the ENTER Key's HOTKEY function tho)
  • Looks like the VA is _really_ unique.
    Its not working for the VA. It seems that the VA is not recognising the OnKeyPressed and onKeyReleased functions..

    onKey(evt) works fine..

    the following are the Debug logs

    onKeyPressedKey == returns the keyNum from evt.getKey
    onKeyReleasedKey == returns the keyNum from evt.getKey

    Without Pressing anything:
    keyDur=0; onKeyGetKey=null; LOCK=0; onKeyReleasedKey=null; onKeyPressedKey=null

    (short) Press the Upper RIght Hand Key:
    keyDur=0; onKeyGetKey=4; LOCK=0; onKeyReleasedKey=null; onKeyPressedKey=null

    (Long) press the Upper Right hand Key:
    keyDur=0; onKeyGetKey=null; LOCK=0; onKeyReleasedKey=null; onKeyPressedKey=null


    Garmin - need your help with actual device and knowledge of what's happening.
  • Brandon / Brian - can u guys take a look at this and revert accordingly?

    The code works on a F3 just fine. But not on the VA at all.