How do you check how long a key has been held down?

This forum is currently in read-only mode.
  • I would like to store (can be in a private variable or global variable) the duration (can be in seconds or milliseconds) which a certain key has been continuously held down since it was last pressed.

    What is the way I should do this?

    Thanks.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • + MouseKeyboard: Key Space is down
        + System: Is global variable 'Timestamp' Equal to 0
        -> System: Set global variable 'Timestamp' to Timer
        + System: Else
        -> System: Set global variable 'Duration' to Timer - global('Timestamp')
    
    + MouseKeyboard: On key Space released
    -> System: Set global variable 'Timestamp' to 0
    -> System: Set global variable 'Duration' to 0
    
    + System: Always (every tick)
    -> EditBox: Set text to global('Timestamp') & " : " & global('Duration')[/code:2bb1jypp]
    
    Copied as text from Construct, the above is probably how I'd do it. The indented lines are sub-events. The Editbox was just a simple way to display the variables in real time.
    
    Depending upon your needs, you may not need two variables. The above will need the timestamp to be stored, at least.
    
    You could also use a variation that adds TimeDelta to a duration variable every tick while the key is down, and sets it to zero when released...
  • Here is what I would do. I would make a private variable and say

    MouseKeyboard:Button is down>Add to Timer 1*timedelta

    http://dl.dropbox.com/u/7605789/SpaceISdownTIMER.cap

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)