一个getInputMap的多个键

时间:2013-04-20 18:08:42

标签: java key-bindings

当我同时按下2个按钮时,我正在寻找一种使用某种方式的方法。实际上,如果我先按D(用于移动)然后按S(用于跑步),则角色可以运行。但是当我尝试先按S然后按D时,角色根本不会移动。我这样做了:

GameWindow.eng.getInputMap().put(KeyStroke.getKeyStroke("S D"), "Run and move");

GameWindow.eng.getActionMap().put("Run and move", new AbstractAction(){
    private static final long serialVersionUID = 8491492566619071329L;

    @Override
    public void actionPerformed(ActionEvent e){
        System.out.println("Hi!")
    }
});

但它似乎不起作用。同样适合跳跃。请帮忙!

1 个答案:

答案 0 :(得分:2)

正确使用Key Bindings。这是一个简短的例子来说明如何使用它们。

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.KeyStroke;

public class KeyBindingsTest {
    JFrame frame = new JFrame();

    public KeyBindingsTest() {
        ActionMap actionMap = frame.getRootPane().getActionMap();
        InputMap inputMap = frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

        for (Keys direction : Keys.values()) {
            actionMap.put(direction.getText(), new KeyBinding(direction.getText()));
            inputMap.put(direction.getKeyStroke(), direction.getText());
        }

        frame.getRootPane().setActionMap(actionMap);
        frame.getRootPane().setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, inputMap);
        frame.setVisible(true);
    }

    private class KeyBinding extends AbstractAction {
        private static final long serialVersionUID = 1L;

        public KeyBinding(String text) {
            super(text);
            putValue(ACTION_COMMAND_KEY, text);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            String action = e.getActionCommand();
            System.out.println("Key Binding: " + action);
        }
    }

    public static void main(String... args) {
        new KeyBindingsTest();
    }

    private enum Keys {
        ESCAPE ("Escape",    KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)),
        CTRLC  ("Control-C", KeyStroke.getKeyStroke(KeyEvent.VK_C,      KeyEvent.CTRL_DOWN_MASK)),
        CTRLP  ("Control-P", KeyStroke.getKeyStroke(KeyEvent.VK_P,      KeyEvent.CTRL_DOWN_MASK)),
        UP     ("Up",        KeyStroke.getKeyStroke(KeyEvent.VK_UP,     0)),
        DOWN   ("Down",      KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,   0)),
        LEFT   ("Left",      KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,   0)),
        RIGHT  ("Right",     KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT,  0));

        private String text;
        private KeyStroke keyStroke;

        private Keys(String text, KeyStroke keyStroke) {
            this.text = text;
            this.keyStroke = keyStroke;
        }

        public String getText() {
            return text;
        }

        public KeyStroke getKeyStroke() {
            return keyStroke;
        }

        @Override
        public String toString() {
            return text;
        }
    }
}