Get the last key pressed down

I’m making a rythm game where the player uses the numpad keys (0-9) as the input. The problem, as stated in this post, is that I can’t get the last pressed key with LastPressedKey() while another key is held down.

The numbers that would need to be pressed in order are stored in an array like this:

[1, 2, 3, -1, -1, 3, 2, 1, -1, -1, 2, 5, 8, ...]

where the numbers fall on the beats at the specified bpm, and -1 means a rest.

Any ideas on how to check if the last key pressed is the same as the corresponding one in the array even if other key(s) are held down?

Is that something you noticed? Because your link is from 2015, the engine has changed a lot in the meantime.
Maybe you can store each new keystroke in a text object, and update the text as the keys are pressed/released, using string manipulation expressions. Text manipulation - GDevelop documentation

1 Like

Okay, so I looked into it deeper and what I noticed is quite interesting:

LastPressedKey() does indeed return the last key pressed even when other keys are pressed down, except for some cases. It might be keyboard specific, but if I hold down Num1, Num2 and Num3, the keys q, w, e, a, s, d, z, x and c (so all the keys that are kind of in the same coloumn) don’t get registered. But with other combinations I can hold down up to 8 keys at the same time.

The problem I have now is how do I increment a variable when a new key was pressed?

That actually sounds like your keyboard doesn’t support multikey rollover for those keys. This is unfortunately very common for most keyboards nowadays.

Even mechanical keyboards don’t always support full multikey rollover for all keys.

1 Like

I tested and Googled as well. Basic keyboards can’t do a lot of keys at a time. Mine can only do a couple of upper numbers but can do most of the number pad at the same time. With letters, I can get about 5 in a group. I used [key is press] and went through a string to test each key and then added it to a string. Numbers and other characters need extra letters like Num1 or something.

1 Like

Okay, so I think every keyboard can handle 3 keys held down at the same time so that’s no problem, I can use lastPressedKey().
The problem I still have is this:

[1, 2, 3, 4, 1, 1, 3, 5, ...]

Checking inputs works fine until there is two or more of the same number in the array, because then the idea of comparing the currently pressed key to the previous one fails.
I’m not necessarily interested in the exact solution but more like the algorithm behind it.

So, you have an array with:
1- 1
2 - 1
3 - 2

You can check the value (right side) but also the name/reference (left side).
You could increment a variable to keep track of the number of keystrokes.
But what if the player is late and skips one?..

You can delete from the array with each successful keystroke, so there are no more previous strokes.
And I suppose you can decide a timeframe for each stroke, and use a timer to delete the latest number from the array, in case the player skips a stroke. :thinking:

1 Like

Well thank you, I haven’t thought of this. I got it working now.

1 Like