• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Detect Last 3 Simultanously Pressed Keys

poliver

Member
OK, it's not hard to detect simultaneous key presses if you know which keys to expect.

but
I'm losing my mind trying to figure out how to get just the last keypresses.

I'm trying to detect last max 3 simultaneously pressed keys.

The built in variable keyboard_key returns a numerical code and is triggered i guess every frame.
The keyboard check functions use vk_ and ord("x") synthax.


What i've tried is detecting when keyboard_key is not 0 for the first key_press
THIS WORKS

AFTER THIS I DON'T KNOW WHAT TO DO
I want to break detection if
  • three keys been pressed
  • this first key is released =>
but I can't do that cause I'm getting back a keycode not a vk_ or ord(""). I've tried feeding the keyboard_check_released function a keycode but it's not working

I want to detect the second key press
for this i check again if keycode != 0 and if keycode != first_key

THIS DOESN'T WORK DURING A FAST SIMULTANEOUS PRESS. ONLY DETECTS IF I DO IT SEQUENTIALLY.

doing same for third key

if keycode != 0 and if keycode != first_key and if keycode != second_key

THIS DOESN'T WORK DURING A FAST SIMULTANEOUS PRESS. ONLY DETECTS IF I DO IT SEQUENTIALLY.

I do have a generic keyboard but 3 keys should be fine. It works in other software so this must have something to do with my code.
 

Mert

Member
for alphabetical letters, you can use keyboard_string

something like;
Code:
var a = string_copy(keyboard_string,string_length(keyboard_string)-1,1);

//a is the last key.
You can store this in a ds_list or ds_queue to have some sort of keyboard recording algorithm. For function keys, I believe you have to add all of them into a list/queue from their respective events.
 

kburkhart84

Firehammer Games
I personally would just loop through them. You can call keyboard_check() or whatever it is for most of the 256 keycodes easily enough, and have an array store the returns...or you can just store which ones get pressed down. Then, you either iterate over the array to see if 3 are down, or you just track your list of which ones are down. Of course, you also have to track which ones get released at any given time.

1. Loop through all the keys, and each time you find one that is down, add that value to a list, if it isn't already in the list.
2. Loop through the list itself, and for each value, see if it is still down, and if so, remove it from the list.
3. Run the logic of checking the list if it is at least 3 units big, and if so, what are the 3 values.

This method should also get around if you press multiple keys at a time at the exact same time, as you aren't stopping the loop when you find one, rather you just add it to the list and keep going. However, it does mean that if someone presses 4 keys or more that are picked up, the list will contain all those values this frame, and your logic will need to account for that, either just picking the first 3, or blocking until they release one of the 4 keys, or however you want to handle it.
 

poliver

Member
Thank you for the tips guys.
Also most keyboards don't allow for various combinations of 3 or more keys, which GM can't help you with, as it's a hardware issue.
I've noticed that. I though cheaper keyboards just don't allow more than 3 keypresses.
But when I'm just printing the keycode variable i can see that in some cases it's max 2 alphabetical keys (most of the time if they're close to each other, must something to do with how they're wired)
With a modifier keys i can get 3 easiely
 
Top