3

I am looking for a way to track the duration of a key press in gamemaker. I was looking into alarm functions, but I figured there's a more elegant way. How can I track the duration of a keypress?

House
  • 73,224
  • 17
  • 184
  • 273
miasyntax
  • 33
  • 3

1 Answers1

2

You could simply have a variable that you will increment each step if the key is currently down, and read its value once the key is released.

Create event:

keyTimer = 0;

Step event:

if (keyboard_check(ord('A'))) {
    keyTimer += 1;
}

Key A Released event:

timePressed = keyTimer;
keyTimer = 0;

In this example, I am reading the time the user has pressed the A key and putting the value in the timePressed variable.

Alexandre Desbiens
  • 1,554
  • 1
  • 12
  • 18