Too many keypresses!

Had a question… everything works great in NumberWizard and behaves as demonstrated in the video… except one thing!

When I hit the up or down arrow, or even the return key, multiple key presses are recorded. No matter how lightly I try to tap the key, it rarely records only one press!

I noticed in the video it was recording just one time each press.

What is happening, and why is it different behavior even though the same code?

Thanks for any help!

Be sure to check if your not using the command Input.GetKey instead of Input.GetKeyDown, as this can cause such behavior. Input.GetKey returns true each frame you hold the key, so for example 30-40 fps. Even if you just tap the key it will be long enough for few frames to pass, ang thus record it a few times. Input.GetKeyDown on the other hand “returns true during the frame the user starts pressing down the key”, so no matter how long you hold it it will just record one time the moment you start pressing the key. Hope that helps, and hope it was the problem.

1 Like

By jove, you’ve done it! I’m not sure how I even did that by accident, since it was not a syntax problem I must have just glanced over it.

Thank you so much!

I’m having the same issue, but im using (Input.GetKey(KeyCode.UpArrow))

I don’t understand why

Hi there, :slight_smile:

You should use Input.GetKeyDown instead of Input.GetKey, as explained by Janusz in his answer.

An intuitive explanation of the difference between Input.GetKeyDown and Input.GetKey, is that Input.GetKeyDown returns “True” when it senses the movement of the key going down. On the other hand, Input.GetKey returns “True” as long as the key is pressed.

Input.GetKeyDown is called in the Update function. The Update function is called 60 times per second. When you press a key, it goes down only once, hence Input.GetKeyDown returns “True” only one time. But as a human when you press a key, it takes approximately half a second before you release it. In the meantime, the update function is then called approximately 30 times. Hence, if you use Input.GetKey, it will return “True” approximately 30 times.

Long story short, Input.GetKeyDown is the way to go! :wink: I hope this helps you.

1 Like