GML Tapping Keys without Holding [SOLVED]

P

Patrick Johnson

Guest
Hey all,

I need some help with keyboard tapping.

What I want to do:
Quickly Tap the space key on a keyboard to launch the start menu
Quickly Tap the space key on a keyboard again to close the start menu

What I have figured out already:
How to execute/stop the script itself

What I have tried (but isn't working):
button timers in association with keyboard checks
alarms

Right now all I can figure out is to hold the key to keep the script running. keyboard_check/_pressed/_released doesn't seem to work the way I want it to. I can only hold the key down to keep it open, and it closes once I release the key. I know the solution is probably simple but I'm scratching my head over it.

Help is much appreciated!

Cheers
 

samspade

Member
You'll have to code key tapping yourself. Fortunately, it isn't hard. The logic is this:
  • When key is held, count up
  • When key is released, check to see if value is less than the tap length you want
  • if it is, do something
  • either way reset variable to 0
Example:

Code:
///step event
if (key_held) {
    key_held_time += 1;
} else if (key_released) {
    if (key_held_time <= tap_length) {
        //you've tapped
    }
    key_held_time = 0;
}
It doesn't need to be done exactly that way (e.g. you could put it in a key pressed event, you could use the built in alarms - starting it on key pressed, and so on) but the logic is the same regardless.
 
T

Taddio

Guest
So you want the menu to open when yyou press a key. Then it stays open until you press it again without needing to hold it down, right?

How I usually tackle that is using a flag for the menu, e.g. (in create event)
Code:
menu_is_open = false;
Then in Key Pressed event:
Code:
menu_is_open = !menu_is_open;

//Will trigger on/off the menu
And finally, in some obj_menu, obj_controller, or something like that:
Code:
if(menu_is_open) {
    //Draw the menu and all the stuff related to it
}
Hope that's what you were looking for!
 
P

Patrick Johnson

Guest
I got it working! I wasnt using the key_check_pressed event properly. Thanks all.
 
Top