[SOLVED] Waiting until a keyboard key is released

Started by visitingpeanut48, Wed 18/03/2020 17:53:53

Previous topic - Next topic

visitingpeanut48

In essence what I want to do is this: press Esc to open the pause menu, and then press Esc again to close it.
I have achieved this by using the following if-statement in the repeatedly_execute() function (if there is a better way please tell me):
Code: ags

if(IsKeyPressed(eKeyEscape)){
	gPauseMenu.Visible = !gPauseMenu.Visible;
}

Of course, the issue here is that the function executes several times.

I've tinkered a (very) small amount with C before (in the context of microcontrollers), and how I would've solved similar situations is by adding a while-loop:
Code: ags

if(IsKeyPressed(eKeyEscape)){
	gPauseMenu.Visible = !gPauseMenu.Visible;
	while(IsKeyPressed(eKeyEscape)){}
}

This way we would only move on when the user has released the key.
Sadly, this doesn't work in AGS because the game closes after some number of loops.

A hack solution would be to use Wait() in order to make the number of loops stack slower:
Code: ags

while(IsKeyPressed(eKeyEscape)){
	Wait(1);
}

This way, you'd need to hold down Escape for about an hour for the game to close.
However, calling Wait() makes the mouse cursor disappear for the duration waited, and that is freaking ugly.

So my question is this: can you comfortably wait until a keyboard key is released?

also if it turns out I just missed a better function that does exactly what I want I will be very upset

Laura Hunt

#1
I put mine in the on_key_press function that comes predefined in the global script:


Code: ags
function on_key_press(eKeyCode keycode) 
{  
  if (IsGamePaused()) {
    
    if (gQuitGui.Visible && keycode == eKeyEscape) { //close quit GUI with Esc key
      gQuitGui.Visible = false;
      return;
    }
    
    else keycode = 0; // any other key is ignored if the game is paused
  }
  
  if (keycode == eKeyEscape) gQuitGui.Visible = true;  // the gui is set to pause the game when visible
  
}


I'm sure there's a more elegant / optimal way to do it, but this works!

visitingpeanut48

Quote from: Laura Hunt on Wed 18/03/2020 18:41:53
I put mine in the on_key_press function that comes predefined in the global script

Yeah I just realized you could do that! You're right, it ain't pretty, but at least it does the job.
Of course the gui still flickers if you hold down the key, but I guess you can't have everything

TheManInBoots

#3
To avoid the blinking you could try this:

you declare an int in Global Variables or at the top in the script body:

Code: ags
int timer;


In the on key function you write:

Code: ags
function on_key_press(eKeyCode keycode) 
{

if(keycode==eKeyA&imer<1)
{
gInventory.Visible = !gInventory.Visible;
timer  ;//(I'm not sure if it's being displayed correctly but I wrote "timerPLUSPLUS;" Sometimes the plus symbols are not displayed
}
}


Then in repeatedly execute always:

Code: ags

if(IsKeyPressed(eKeyA)==false)timer=0;


Unless you have a lagging game, normally this should not blink anymore.



SMF spam blocked by CleanTalk