3

I'm programming a game in java that has two players on the same keyboard. If one of the players is holding their forward button and the second player presses his forward button the first player is stopped. This happens the other way around too and I'm not sure how to fix it.

  addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {

        }
        @Override
        public void keyPressed(KeyEvent e) {
            paddle.keyPressed(e);
            paddletwo.keyPressed(e);
        }
        @Override
        public void keyReleased(KeyEvent e) {
            paddle.keyReleased(e);
            paddletwo.keyReleased(e);
        }

In each paddle class I also have this:

public void keyReleased (KeyEvent e) {
    ya = 0;
}

public void keyPressed(KeyEvent e) {
   if(e.getKeyCode() == KeyEvent.VK_UP)
       ya = -pong.speed;
   if(e.getKeyCode() == KeyEvent.VK_DOWN)
       ya = pong.speed;
}

Player two uses VK_W and VK_S instead of UP and DOWN.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
TKDefender
  • 41
  • 6

1 Answers1

0

This is related to the actual hardware keyboard. To reduce costs the control lines are shared so that it's impossible to push a certain combination of keys at the same time.

Either buy a high-end keyboard or choose the keys carefully so there is minimal interference (of course this is harder the more keys you use, and it will result in some funny looking controls).

Here's the reason explained.

Edit: Actually this shouldn't affect when only 2 keys are involved, only with 3 or more.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • But there should not be a problem if you are testing just 4 keys (2 of them on the right site of the keyboard, 2 of them on the left side. – danielspaniol May 12 '15 at 07:33
  • What are you basing that on? – Kayaman May 12 '15 at 09:19
  • In his question he says, that paddle1 tests VK_W and VK_S and paddle2 tests VK_UP and VK_DOWN. For normal 4 keys or more at the same block should cause problems, but 3 keys on the left side and 3 keys on the right side should be ok (I tested it at the keyboard of my laptop, and it really is not a high-end keyboard, and I press 3 keys with my left hand and 3 keys with my right hand and it still managed to get them all) – danielspaniol May 12 '15 at 10:52
  • That only tells you that your keyboard doesn't have the problem with those specific keys. – Kayaman May 12 '15 at 11:01
  • No, I tested it with several keys and you could clearly see, that the problem comes when too many keys on the same side were pressed. Also the keys are arranged in "blocks" and a keyboard can't recognize too many keys in the same "block" – danielspaniol May 13 '15 at 06:33