1

I have recently started to work on the user interface of a primitive game I've been creating. It has gone well with one exception. I am currently using the Keystroke class as a way getting input instead of Key Event.

public void bindControls()
{
    inputMap = frame.getMainPane().getInputMap(IFW);
    actionMap = frame.getMainPane().getActionMap();
    inputMap.put(KeyStroke.getKeyStroke("UP"), Controls.UP_ACTION);
    inputMap.put(KeyStroke.getKeyStroke("DOWN"), Controls.DOWN_ACTION);
    inputMap.put(KeyStroke.getKeyStroke("LEFT"), Controls.LEFT_ACTION);
    inputMap.put(KeyStroke.getKeyStroke("RIGHT"), Controls.RIGHT_ACTION);
    inputMap.put(KeyStroke.getKeyStroke("W"), Controls.UP_ACTION);
    inputMap.put(KeyStroke.getKeyStroke("S"), Controls.DOWN_ACTION);
    inputMap.put(KeyStroke.getKeyStroke("A"), Controls.LEFT_ACTION);
    inputMap.put(KeyStroke.getKeyStroke("D"), Controls.RIGHT_ACTION);
    actionMap.put(Controls.UP_ACTION, new MoveAction(Controls.UP_ACTION));
    actionMap.put(Controls.DOWN_ACTION, new MoveAction(Controls.DOWN_ACTION));
    actionMap.put(Controls.LEFT_ACTION, new MoveAction(Controls.LEFT_ACTION));
    actionMap.put(Controls.RIGHT_ACTION, new MoveAction(Controls.RIGHT_ACTION));
}

This is the method that I run to bind the controls for the Player. My issue is that "W", "A", "S", and "D" all work fine, however "UP", "DOWN", "LEFT", and "RIGHT". Do nothing when pressed. I've looked everywhere I can including the java docs. But everything I've read claims that the code above is correct.

If anyone has had this issue or has a fix i could use your help. Thanks!

P.S. Controls.java is an enum

package controls;

public enum Controls 
{
    UP_ACTION, DOWN_ACTION, LEFT_ACTION, RIGHT_ACTION
}

Edit: My MoveAction Class

package controls;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

public class MoveAction extends AbstractAction
{
    private static final long serialVersionUID = 1L;
    Controls direction;
        public MoveAction(Controls direction)
        {
            this.direction = direction;
        }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(direction == Controls.UP_ACTION)
        {
            up();
        }
        else if(direction == Controls.DOWN_ACTION)
        {
            down();
        }
        else if(direction == Controls.LEFT_ACTION)
        {
            left();
        }
        else if(direction == Controls.RIGHT_ACTION)
        {
            right();
        }

        System.out.println(direction);
    }

    public void up()
    {

    }

    public void down()
    {

    }

    public void left()
    {

    }

    public void right()
    {

    }
}
DaBenjle
  • 23
  • 7

1 Answers1

1

Thanks to @Zachary for the answer. It is

(KeyEvent.VK_UP, 0) 

not

("UP")

I still don't know why this is the case, as it both seem to work for other users. But this works so I'll take it.

DaBenjle
  • 23
  • 7