[Solved] Check if a key was just pressed

How can I check if any key was just pressed?
The Any key pressed condition always returns true while a key is down, but I need a condition to return true only once, right after a key was pressed down.
It looks like a simple thing, what am I missing?

Add a “Trigger once while true” condition.

There are 2 problems with that:

  1. It only works for 1 key at a time
  2. It will still return true even if a key was pressed down earlier.

Specifically I need to check if there was a keystroke in a given time interval, for example like this:

if (timer > 0.3 and timer < 0.6 and a key was pressed down):
then do something

If you have a time interval, you’ll need to use a timer, of course. :person_shrugging:

Okay, so maybe I wasn’t clear enough, here is the relevant code:


Currently this is what this does:
When the timer is in between the specified interval, it will check for any pressed down keys, meaning if I start to press a key ahead of time, before the interval, then the actions will execute.

What I want is something instead of “Any key is pressed”, that checks if there is a new keystroke in the specified time interval.

If I understand it correctly, what you’re after is if you are holding down a key before the time interval starts, you want that keypress ignored, and only new ones that occur during the time interval to be ‘registered’.

If I’m correct on this, then you’ll need to keep track of what keys are pressed before you get to that time interval. The screen snip below does that by adding or removing a structure variable element for each key press/release. I found it struggles if you press more than 2 keys simultaneously, but that could be because I’m running it in a VM with a bit of lag:

1 Like

Hi,

Here is a solution:

Two events after and outside your interval event:

  • if any key pressed , then set Boolean variable “keypressed” to TRUE
  • if any key pressed, inverted condition (so it means if not a single key is pressed), then set Boolean variable “keypressed” to FALSE

In your interval event, you add the condition “Boolean variable keypressed is FALSE”

This way, if some keys are pressed before entering the interval of time, it won’t work. But if entering the interval it will work once, and immediately after, in the same frame, it will set the Boolean to TRUE. So at the next frame it won’t trigger the interval event a second time. Hence the “trigger once” in the interval event is no longer needed.

I think it’s an easy solution, but tell me if I’m wrong somewhere.

1 Like

What is the UI going to look like? Are you displaying the note/key as text or animatiin?

This is just me. I’m imagining something like the guitar hero genre where there’s moving squares that represent notes and timing. You could use collision to check for timing and accurate keys. This might help even if you weren’t using the design. You could use hidden rectangular sprites. It wouldn’t have to be 3D, it could be simple moving rectangles like player piano sheet music.

1 Like

@MrMen your solution works great, so now I have a struct of keys currently pressed down.
But what condition do I put in the time interval to check for a new key?

@Bluzima the problem with your approach is that putting the “Boolean variable keypressed is FALSE” condition in the interval will only work if at least one key is pressed; if no key is pressed, it will immediately run the actions.

image

2 Likes

Thank you @MrMen for your solution, in works now!