3

I have a class InputHandler which implements the libGDX InputProcessor. It is used to handle arrow key and space bar events.

The keyDown and keyUp methods:

@Override
public boolean keyDown(int keycode) {

    switch (keycode) {
        case Keys.SPACE:
            gameWorld.setSpacePressed(true);
            break;
        case Keys.LEFT:
            gameWorld.setLeftPressed(true);
            break;
        case Keys.RIGHT:
            gameWorld.setRightPressed(true);
            break;
        case Keys.UP:
            gameWorld.setUpPressed(true);
            break;
        case Keys.DOWN:
            gameWorld.setDownPressed(true);
            break;
        default:
    }
    return true;
}

@Override
public boolean keyUp(int keycode) {

    switch (keycode) {
        case Keys.SPACE:
            gameWorld.setSpacePressed(false);
            break;
        case Keys.LEFT:
            gameWorld.setLeftPressed(false);
            break;
        case Keys.RIGHT:
            gameWorld.setRightPressed(false);
            break;
        case Keys.UP:
            gameWorld.setUpPressed(false);
            break;
        case Keys.DOWN:
            gameWorld.setDownPressed(false);
            break;
        default:
    }
    return true;
}

My problem is that if all three of the up arrow, left arrow and space bar keys are pressed, the last to be pressed does not trigger an event. However, the right and down arrow keys function perfectly.

Examples:

  • Left arrow + space bar: Both setLeftPressed and setSpacePressed are triggered.
  • Left arrow + up arrow + space bar: setSpacePressed is not triggered.
  • Right arrow + up arrow + space bar: All three are triggered.
  • Left arrow + up arrow + down arrow: All three are triggered.
  • Up arrow + space bar: Both setUpPressed and setSpacePressed are triggered.

Thanks in advance for any help.

Community
  • 1
  • 1
AkraticCritic
  • 2,595
  • 2
  • 12
  • 11

1 Answers1

1

This may not be because of your code or Libgdx, it may be an issue with your keyboard. Sometimes, in most average keyboards, 3 (or more) keys being pushed at once will not work if they are close together. This phenomenon is known as keyboard ghosting. I've noticed that the keys not working for you are relatively closer to each other (in physical distance) compared to the other combinations. Try using that same combinations in the link provided, and maybe try using another keyboard/machine to test that code. If the problem lies in your keyboard, you might want to consider letting your code support variable key definitions instead of hard-coded ones for anyone using a similar keyboard.

Abby
  • 46
  • 2