Key press ignored if key is released at same time

Hey guys! I am trying to create a sketch where an object on the screen will move when the WASD keys are pressed. The object exists on a grid and will to the centre of a square adjacent to its own in a direction according to which key has last been pressed.

The issue I am having is when a key is released at the same time a new key is pressed (possibly only on the exact same frame). It seems the sketch will wait for the “key repeat” to kick in until the new direction is recognized. Here is the code I am using to send input to the moving object’s movement function. I can paste the rest of the sketch if needed but I would like to know if the issue is arising from this part of the code. I am using a string to store the direction variable, I know this is not optimal but was not sure what I should use.

void keyReleased() {
  if (key == 'w'||key=='d'||key=='s'||key=='a') {
    player1.d = "NONE";
  }
}

// detect key presses
void keyPressed() { 
  switch(key) {
  case 'w':
    player1.d = "UP";
    break;
  case 'd':
    player1.d = "RIGHT";
    break;
  case 's':
    player1.d = "DOWN";
    break;
  case'a':
    player1.d = "LEFT";
    break;
  }
}

So the reason why I think you’re having some latency could be because you are setting your player1.d variable and such, and I think that’s a little data heavy. Especially the part where you set it back to “NONE” every time a key is released. In that case you have something like this going on:

player1.d = “NONE”
W Key pressed:
player1.d = “UP”
W Key is Immediately released
player1.d = “NONE”

And most likely, any code that you have checking what player1.d even is, isn’t getting executed in the fraction of a second it takes for you to press and release your key, and if it is, it will see player1.d as “NONE” because you had it be set to that as soon as the key is released, so your direction won’t even be processed. Hopefully that makes sense?

What I would recommend is doing this:

//somewhere in your player1 class: 
int x = 30; //just an EXAMPLE
int y = 30; //again, just an example


//and then down here you would do:
void keyPressed() { 
  switch(key) {
  case 'w':
    player1.y--; //move your player up 
    break;
  case 'd':
    player1.x++; //move your player to the right
    break;
  case 's':
    player1.y++; //move your player down
    break;
  case'a':
    player1.x--; //move your player to the left 
    break;
  }
}

That should get you started? Feel free to post more code to show your progress and if you have any more questions I would love to help out!

EnhancedLoop7

Studio.ProcessingTogether.com/sp/pad/export/ro.91tcpPtI9LrXp

2 Likes

Thanks for your reply!
I’m not sure if I made my problem clear enough. If I tap a movement key quickly it works just as intended. The problem arises is when I let go of one key and press down on another very quickly. If I am holding one key then begin to hold another without releasing the first the new direction is recognized fine. It’s only when I make effort to release one and press another as quick as I can that the movement stalls, and its not like a lag, it definitely feels like the movement begins again once the key repeat kicks in. Its almost as if the key release will override the new pressed keycode.

Also, surely any code I execute in the draw() method should process all within the same frame? If there was a lag from too much data the frame rate would slow down, rather than the program waiting until the next frame to change the variables?

Thanks! very helpful.

In that case, I think @GoToLoop answered your question best :smiley:

The most important part is having a variable be set once a key is pressed or released, which is what you wanted to do, and of course there are different ways of doing such :joy:

I’m glad you got that working though, and of course if you have any more questions, we would love to continue helping out,

EnhancedLoop7

2 Likes