1

As the title suggests, in my Java game, I cannot detect if I'm pressing the spacebar and other keys at the same time.

For example, spacebar is the shoot key, and the arrow keys make the player move. If I am pressing the up arrow key and the left arrow key and the spacebar, then it should shoot a bullet up and to the left.

However, after using multiple System.out.println(); to debug, I've found that if I am pressing two keys, it is not detecting the spacebar if it's pressed.

public void keyPressed(KeyEvent e) {


if(e.getKeyCode() == 32){
    pressingSpacebar = true;
    System.out.println("Spacebar pressed true");
}


    // Up arrow key
    if(e.getKeyCode() == 38){
        up = true;
        System.out.println("Up = true");
    }
    // Down arrow key
    if(e.getKeyCode() == 40){
        down = true;
        System.out.println("Down = true");

    }
    // Right arrow key
    if(e.getKeyCode() == 39){
        right = true;
        System.out.println("Right = true");
    }
    // Left arrow key
    if(e.getKeyCode() == 37){
        left = true;
        System.out.println("Left = true");
    }
}

Then in keyReleased:

public void keyReleased(KeyEvent e) {
    if(e.getKeyCode() == 38){
        up = false;
        repaint();
    }
    if(e.getKeyCode() == 40){
        down = false;
        repaint();
    }
    if(e.getKeyCode() == 39){
        right = false;
        repaint();
    }
    if(e.getKeyCode() == 37){
        left = false;
        repaint();
    }
    if(e.getKeyCode() == 32){
        pressingSpacebar = false;
    }
}

This is how I am checking if you are pressing the spacebar and multiple keys:

if(pressingSpacebar){
        if(right == true && down == true && up == false && left == false){
            // Shoot bullet
        }
}   

Why isn't the spacebar being detected? If I don't check for spacebar being pressed the bullets shoot fine, but when I check for the spacebar it just doesn't detect it.

Note: I have read other posts similar to this question but the answers were not very helpful. I am a sorta newbie when it comes to this stuff so try to give a simple answer or explain it a bit. Thanks in advance!

joey942
  • 159
  • 2
  • 11
  • 1
    You should check : http://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time – GoldenBolt Dec 17 '16 at 18:50
  • GoldenBolt is right, this is a duplicate. Two keys are pressed at the same time if you have two KeyPressed events without a KeyReleased event. – Sentry Dec 17 '16 at 18:52
  • Better to use [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) as per [MadProgrammer's answer to this question](http://stackoverflow.com/a/19823986/522444) – Hovercraft Full Of Eels Dec 17 '16 at 18:53
  • Have you tested the keyboard itself? Some low-end keyboards will output only one keycode despite more than one key being pressed. If you don't know if that could be the case, I'd recommend checking that first. – walen Dec 17 '16 at 19:01
  • @walen Yeah I just saw that, I'll try it out and let you know. – joey942 Dec 17 '16 at 21:39
  • @GoldenBolt I'm looking at the answer in that and I don't understand it. I see that it's checking how many keys are being pressed but how does that solve the problem? It adds the keys pressed to a HashSet, but where do I do all of my keycode checks? Inside of the if (pressed.size() > 1) ? – joey942 Dec 17 '16 at 21:56

3 Answers3

1

A KeyEvent is only generated for the last key pressed, so you need to keep track of a key when it is pressed (for example by adding it to a HashMap) and then on a keyReleased you need to remove the key from the HashMap.

The better way to do this is to use Key Bindings (not a KeyListener) to bind the KeyStroke to an Action.

Check out the KeyboardAnimation example found in Motion Using the Keyboard. It explains more about Key Bindings and provides a full working example of handling the Up/Down/Right/Left keys. In general any two keys can be held down at one time to give diagonal motion.

The logic will also work with 3 keys, but as has already been mentioned the keyboard itself may not support that many keys pressed at one time.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I'll check it out when I'm home, but I recently saw a post on another website with the exact same problem. They could press up left and spacebar and it was fine but stuff like up right and spacebar wasn't working. Apparently he discovered it was something wrong with his keyboard. Could this be the case? – joey942 Dec 17 '16 at 20:59
0

I've encountered a similar problem. I found out that there is no guarantee on that simultaneously pressing keys and holding them will trigger key pressed event more than once for each key. So if you're relying on that you need to keep that in mind.

Yan.F
  • 630
  • 5
  • 20
-1

I think you should try all conditions in a single construct itself.

Try it -

if(pressingSpacebar && right && down && !up && !left){
            // Shoot bullet
}
  • Unfortunately I have the same outcome. However I will do it this way since it looks a lot cleaner. – joey942 Dec 17 '16 at 18:47