1

当我同时按下 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!")
    }
});

但这似乎不起作用。跳跃也是一样。请帮忙!

4

1 回答 1

2

正确使用键绑定。这是一个简短的示例来展示如何使用它们。

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;
        }
    }
}
于 2013-04-20T18:11:09.930 回答