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

For questions about using Classic.

Post » Tue Nov 16, 2010 3:07 pm

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.
B
5
S
1
G
3
Posts: 59
Reputation: 993

Post » Tue Nov 16, 2010 4:44 pm

[code:2bb1jypp]+ 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...
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Mon Nov 22, 2010 10:32 am

Here is what I would do. I would make a private variable and say
MouseKeyboard:Button is down>Add to [u:v3d55u0y]Timer[/u:v3d55u0y] 1*timedelta

http://dl.dropbox.com/u/7605789/SpaceISdownTIMER.cap
B
2
G
2
Posts: 30
Reputation: 710


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 8 guests