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:
- It only works for 1 key at a time
- 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.
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:
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.
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.
@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.