0
\$\begingroup\$

I am new to programming, so forgive me if I make silly mistakes.

I want to make a game, and I started by making the player, and that's where I'm stuck at. I'm trying to use Keybindings (InputMap and ActionMap of a JComponent) to make the player move, but something really odd is happening. When I hold "A" to make the player move to the left, that's what he does. But if I then press "D" to make him move right, he just stands still. Then, if I release and press "D" again, it does move. This happens with all of my keyboards, and I had a similar problem when using a KeyListener.

This seems like a problem with another class, like the Player class, but it isn't: When the situation i described occurs, the AbstractActions I created aren't being called! And that was the same with the KeyPressed() method when I was using a KeyListener.

This is my class for managing keyboard-input:

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

public class KeyManager {

private JLabel keyManager;

private Action leftTrue, leftFalse, rightTrue, rightFalse;

public boolean left = false, right = false, up = false, down = false;


public KeyManager() {
    initActions();

    keyManager = new JLabel();
    System.out.println("called");

    keyManager.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "pressed_D");
    keyManager.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released D"), "released_D");
    keyManager.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "pressed_A");
    keyManager.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("released A"), "released_A");

    keyManager.getActionMap().put("pressed_D", rightTrue);
    keyManager.getActionMap().put("pressed_A", leftTrue);
    keyManager.getActionMap().put("released_D", rightFalse);
    keyManager.getActionMap().put("released_A", leftFalse);
}

public JLabel getKeyManager() {
    return keyManager;
}

private void initActions() {

    leftTrue = new AbstractAction() {

        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("pressed a");
            left = true;

        }

    };
    leftFalse = new AbstractAction() {

        private static final long serialVersionUID = 2L;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("released a");
            left = false;

        }

    };
    rightTrue = new AbstractAction() {

        private static final long serialVersionUID = 3L;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("pressed d");
            right = true;

        }

    };
    rightFalse = new AbstractAction() {

        private static final long serialVersionUID = 4L;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("released d");
            right = false;

        }

    };

}


}

The JLabel object is added to my JFrame in another class

I don't feel like my explanation of the problem made much sense, so here's a video of what's happening:

https://www.dropbox.com/s/7bs6r1kt6z489rf/JavaHelp.mov?dl=0

Does someone know how to fix this? :(

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

uhm i've postet an answer to a very similar question, it's about N-key rollover...

i'm really sorry to post a 'link-only' answer, but have a look at this question from this forum =)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .