Listen to multiple KeyEvents at once?

Last week I had a problem with a small Asteroids clone I was making, I couldn’t shoot while moving and couldn’t move to 2 sides at once…
After a lot of googling and trial and error my KeyListener now looks like this:

public class ArrowListener implements KeyListener{

    private PlayerModel model;
    
    private boolean left = false;
    private boolean right = false;
    private boolean up = false;
    private boolean down = false;
    private boolean space = false;
    
    
    public ArrowListener(PlayerModel model){
        this.model = model;
    }

    public void keyPressed(KeyEvent e) {
        setKey(e.getKeyCode(), true);
        doSomething();
    }

    public void keyTyped(final KeyEvent e) {
    // empty
    }

    public void keyReleased(KeyEvent e) {
        setKey(e.getKeyCode(), false);
        doSomething();
    }

    public void setKey(int key, boolean pressed){
        if (key == KeyEvent.VK_LEFT) {
            left = pressed;
        } else if (key == KeyEvent.VK_RIGHT) {
            right = pressed;
        } else if (key == KeyEvent.VK_UP) {
            up = pressed;
        } else if (key == KeyEvent.VK_DOWN) {
            down = pressed;
        } else if (key == KeyEvent.VK_SPACE) {
            space = pressed;
        }
    }

    public void doSomething() {
        if (left == true) {
            model.left();
        } if (right == true) {
            model.right();
        } if (up == true) {
            model.up();
        } if (down == true) {
            model.down();
        } if (space == true) {
            model.shoot();
        }
    }
}

Is there anyway to do this any better?
Right now, it’s not 100% right, when you’re pressing space and left at once, and let go of the left arrow you stop shooting too…

Yes, there is a better way of doing it:


private byte[] keyCodes = new byte[256];

public void keyPressed(KeyEvent e) {
        keyCodes[e.getKeyCode()] = true;
}

public void keyReleased(KeyEvent e) {
        keyCodes[e.getKeyCode()] = false;
}

public void update() {
        if (keyCodes[KeyEvent.VK_LEFT] && keyCodes[KeyEvent.KEY_UP) {
                 x-=1; y-=1;
        }
}

You shouldn’t do the logic on the AWT thread which the listener runs in…update should just run in a loop on the main thread.

PS. Thats pseudocode i wrote in the forum, not sure if it will work or not, dont take it verbatim, but the spirit is there.

DP :slight_smile:

Thanks a lot for your reply, it works great now, except for 1 minor thing, The left key doesn’t seem to work as good as the right key, very weird…
If I press up, right and spacebar at the same time, it goes to right and forward and it shoots. But if I let go of the right arrow and press to the left, nothing happens. The other way around it works fine ???

Anyway, here’s the game: http://alexandria61.2mhost.com/~mosselst/website/ivo/asteroids.zip
Keep in mind I’m a noob when it comes to java and that I made it in 2-3 hours (not including the time I spent googling the arrow-problem :stuck_out_tongue: )