Is it possible to simulate a key being held down?

E

Estragon Helmer

Guest
I found 'keyboard_key_pressed', and 'keyboard_key_release' which can be used to simulate key presses and releases, but there doesn't seem to be anything to simulate holding down a key.

I tried using the 'keyboard_key', and setting it to the key I want to hold, but it didn't do anything.

Is there any way to do this?
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
@Aura: That checks for key presses. It doesn't simulate them. ^^

@Estragon Helmer: keyboard_key_pressed simulates a key being held down (simulating a press on the exact frame you call it when the key is not already being simulated to be held down), and it does so until you call keyboard_key_release.
 
E

Estragon Helmer

Guest
Ah, okay. I get it now. Thanks guys!

I had an object that had 3 states, and needed to simulate a key being held during the 2nd state.
So I had used keyboard_key_pressed when the 1st state switched to the 2nd,
and then used keyboard_key_release in the 3rd state.
I just needed to add the keyboard_key_pressed into the 2nd state, to keep it going.

Just misunderstood how it worked.
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
keyboard_key_press simulates a key being held down after calling it once. It will keep doing so until you call keyboard_key_release, so there's no need to call it multiple times to simulate a "held" state.
 
E

Estragon Helmer

Guest
keyboard_key_press simulates a key being held down after calling it once. It will keep doing so until you call keyboard_key_release, so there's no need to call it multiple times to simulate a "held" state.
Not sure why that doesn't work for me then. Could be because other objects in the game are checking for the real key press, I guess.
 
A

Aura

Guest
Please post the complete code with context, that is, the events you're putting it in.
 
E

Estragon Helmer

Guest
So, this object is a kind of bouncepad, that just forces the player object to jump when it's on the platform.

This code works perfectly, by the way. It just didn't work unless the "keyboard_key_press (vk_up) ;" line was done every step it needed to be. It's probably not a problem with this code, though. I'd imagine, it'd down to how the player works, and that is WAY too much code for me to show here, and hey, as long as it works, I don't really mind if a tiny bit of the code is messy.

Create event:
state = 1 ;
timer = 0 ;

image_speed = 0 ;​

Step event:
switch (state)
{
case 1:
image_index = 0 ;
image_speed = 0 ;
if (place_meeting (x, y-1, Player)) && (Player.x > bbox_left) && (Player.x < bbox_right) && (Player.death = 0) state = 2 ;
break ;
case 2:
image_speed = 0.3 ;
keyboard_key_press (vk_up) ;
if (image_index >= 3)
{
image_index = 3 ;
timer = 0 ;
state = 3 ;
}
break ;
case 3:
if (timer < 10)
{
keyboard_key_press (vk_up) ;
timer += 1 ;
image_speed = 0 ;
}
else
{
keyboard_key_release (vk_up) ;
image_speed = -0.1 ;
if (image_index <= 0) state = 1 ;
}
break ;
}​

(sorry about the lack of indents, the forum messed them up)

The first state just detects the player on the platform.
The second state animates the platform, and starts the player's jump.
The third state continues, then ends, the players' jump, as well as playing the platform's animation backwards.
 

obscene

Member
You should separate key detection from your movement code.

When a key is pressed... set a variable to true else set it false. Do this in Begin Step.

In your movement code check the variable instead of the key press.

If you want to simulate a key press just override the variable in the Step event before your movement code.
 
E

Estragon Helmer

Guest
You should separate key detection from your movement code.

When a key is pressed... set a variable to true else set it false. Do this in Begin Step.

In your movement code check the variable instead of the key press.

If you want to simulate a key press just override the variable in the Step event before your movement code.
I already do this, but not in the Begin Step event, so I guess that explains it. Currently, I guess that check is overriding the simulated keypress here then.
 
Top