• 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!

{ SOLVED} Problem with pressin multiple key at the same time

RangerX

Member
Hi peeps,
This should be a simple problem to solve. Most probably am forgetting something really stupid. When I press Left key and Right key on the keyboard in very fast succession and hold them down, both the events related to the keys are being played --- while they should not. I thought I was blocking the keys and I even added a keyboard clear command for fun and it didn't fix it. Am at a loss here...


Code:
if(obj_Testing.Gamepad==false)
then
{
    if(keyboard_check(global.K_left) && !keyboard_check(global.K_right))
    then
    {
    keyboard_clear(global.K_right);
    event_perform(ev_other,ev_user0);
    }
  
    if(keyboard_check(global.K_right) && !keyboard_check(global.K_left))
    then
    {
    keyboard_clear(global.K_left);
    event_perform(ev_other,ev_user1);
    }
}
So why both user event 0 and user event 1 would play only when I press the keys in rapid succession??
If you press and hold them at reasonable speed, it works perfectly!!


------------------------- SOLVED -------------------------------

I did block it with variables. Looks like I was too tired yesterday to not think up such an easy thing...

Code:
if(obj_Testing.Gamepad==false)
then
{
    if(keyboard_check(global.K_left) && RightKey_pressed==false)
    then
    {
    event_perform(ev_other,ev_user0);
    LeftKey_pressed=true;
    }
 
    if(keyboard_check(global.K_right) && LeftKey_pressed==false)
    then
    {
    event_perform(ev_other,ev_user1);
    RightKey_pressed=true;
    }
   
    if(!keyboard_check(global.K_left) && LeftKey_pressed==true)
    then
    {
    LeftKey_pressed=false;
    }
   
    if(!keyboard_check(global.K_right) && RightKey_pressed==true)
    then
    {
    RightKey_pressed=false;
    }
}
 
Last edited:
C

CedSharp

Guest
Another option would be to create a variable for both keys, and use keyboard_check_pressed.
This way, you can set or disable the variables based on the "LAST" key pressed.

So if you press both keys almost exactly at the same time, Left will activate,
then Right will activate, which leaves you with only the 'right' variable set to true.
After that, you code checks for the varaibles instead of the keyboard functions :)

Code:
// Create Event
key_left = false;
key_right = true;

// Step Event
if(keyboard_check_pressed(vk_left)) {
  key_left = true;
  key_right =  false;
}
if(keyboard_check_pressed(vk_right)) {
  key_left = false;
  key_right = true;
}
if(keyboard_check_released(vk_left)) key_left = false;
if(keyboard_check_released(vk_right)) key_right = false;

// Event where you test the keys
if(key_left) event_user(0);
if(key_right) event_user(1);
Note that you can use 'event_user( number )' instead of event_perform :)
 

RangerX

Member
Thanks for your ideas you both.
It made me think about something. I am already having a system of variables telling if a putton is pressed or held for gamepad. I just need to do the same thing with my main character here and its gonna be perfect!

Final code in the first post
 
Last edited:
Top