KeyEvents are not accepted on simultaneous key presses

I am programming a game in java that has two players on the same keyboard. If one of the players holds their forward button and the second player presses their forward button, the first player stops. It happens the other way around 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:

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;
}

      

The second player uses VK_W and VK_S instead of UP and DOWN.

+3


source to share


1 answer


It has to do with a real hardware keyboard. To reduce costs, the control lines are split so it is not possible to simultaneously press a specific key combination.

Either buy a high quality keyboard, or choose your keys carefully so there is minimal interference (of course, this is more difficult the more keys you use and this will lead to some ridiculous tweaks).



The reason is explained below.

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

0


source







All Articles