0

Can't handle more then one key at a time in the KeyPressed method. I searched this up, but didn't understand what to do with the way I wrote my code. I would appreciate it if you could show me what to do with my code?

private boolean play = false;

private int player1Score = 0;
private int player2Score = 0;

private int player1Y = 215;
private int player2Y = 215;

private Timer timer;
private int delay = 8;

private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -2;
private int ballYdir = -3;

public GamePlay(){
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    timer = new Timer(delay, this);
    timer.start();
}

public void paint(Graphics g){

    g.setColor(Color.black);
    g.fillRect(1, 1, 692, 592);

    g.setColor(Color.yellow);
    g.fillRect(0, 0, 3, 492);
    g.fillRect(0, 0, 692, 3);
    g.fillRect(691, 0, 3, 492);
    g.fillRect(0, 468, 692, 3);

    g.setColor(Color.white);
    g.fillRect(15, player1Y, 10, 75);
    g.fillRect(670, player2Y, 10, 75);

    g.fillOval(ballposX, ballposY, 20, 20);

    g.setColor(Color.white);
    g.setFont(new Font("serif", Font.BOLD, 25));
    g.drawString("" + player1Score, 175, 30);

    g.setFont(new Font("serif", Font.BOLD, 25));
    g.drawString("" + player2Score, 525, 30);

    if(player1Score >= 11){
        play = false;
        ballXdir = 0;
        ballYdir = 0;
        g.setColor(Color.red);
        g.setFont(new Font("serif", Font.BOLD, 30));
        g.drawString("Player 1 Won", 260, 200);

        g.setFont(new Font("serif", Font.BOLD, 20));
        g.drawString("Press Enter to restart ", 250, 250);
    }
    if(player2Score >= 11){
        play = false;
        ballXdir = 0;
        ballYdir = 0;
        g.setColor(Color.red);
        g.setFont(new Font("serif", Font.BOLD, 30));
        g.drawString("Player 2 Won", 260, 200);

        g.setFont(new Font("serif", Font.BOLD, 20));
        g.drawString("Press Enter to restart ", 250, 250);
    }

    g.dispose();
}
@Override
public void actionPerformed(ActionEvent arg0) {
    timer.start();
    if(play){
        if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(15, player1Y, 10, 75))){
            ballXdir = -ballXdir;
        }
        if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(670, player2Y, 10, 75))){
            ballXdir = -ballXdir;
        }
        ballposX += ballXdir;
        ballposY += ballYdir;
        if(ballposX < 0){
            ballXdir = -ballXdir;
            player2Score += 1;
        }
        if(ballposY < 0){
            ballYdir = -ballYdir;
        }
        if(ballposX > 670){
            ballXdir = -ballXdir;
            player1Score += 1;
        }
        if(ballposY > 460){
            ballYdir = -ballYdir;
        }
    }

    repaint();

}

@Override
public void keyPressed(KeyEvent e) {

    if(e.getKeyCode() == KeyEvent.VK_S){
        if(player1Y >= 390){
            player1Y = 390;
        }
        else{
            moveUp2();

        }
    }
    if(e.getKeyCode() == KeyEvent.VK_W){
        if(player1Y <= 10){
            player1Y = 10;
        }
        else{
            moveDown2();
        }
    }
    if(e.getKeyCode() == KeyEvent.VK_DOWN){
        if(player2Y >= 390){
            player2Y = 390;
        }
        else{
            moveUp();

        }
    }
    if(e.getKeyCode() == KeyEvent.VK_UP){
        if(player2Y <= 10){
            player2Y = 10;
        }
        else{
            moveDown();
        }
    }
    if(e.getKeyCode() == KeyEvent.VK_ENTER){
        if(!play){
            play = true;
            ballposX = 120;
            ballposY = 350;
            ballXdir = -2;
            ballYdir = -3;
            player1Y = 215;
            player2Y = 215;
            player1Score = 0;
            player2Score = 0;

            repaint();

        }
    }

}

private void moveDown() {
    play = true;
    player2Y -= 20;
    repaint();
}
private void moveUp() {
    play = true;
    player2Y += 20;
    repaint();
}

private void moveDown2() {
    play = true;
    player1Y -= 20;
    repaint();
}
private void moveUp2() {
    play = true;
    player1Y += 20;
    repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
tonkos
  • 1
  • Please add a note about what you are actually trying to accomplish. What should multiple key presses do? Can you give a specific use-case? Depending on those details, you can get wildly different answers. – Mad Physicist Apr 17 '17 at 16:36

1 Answers1

0

You could keep track of the pressed keys. Whenever a key is released you remove it from the list of pressed keys. If the list of pressed keys is not empty when a key is beeing pressed, then multiple keys are pressed.

Reto
  • 1,305
  • 1
  • 18
  • 32
  • can you show me an example, using my code. I don't understand what the list is(array list?) and how to remove. would key binding work? – tonkos Apr 14 '17 at 20:12
  • [This post](http://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time) contains the solution you need. – Pavlo Viazovskyy Apr 14 '17 at 22:25