1

我想将ctrl++shiftnumpad 7动作图相关联。基本上,我试图将我的操作绑定到键盘快捷键,并希望与从顶部按下的数字键或从正确的键盘数字按下的数字键具有相同的行为。

我可以映射

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");

但无法映射

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");

这是我要执行的代码:

package testmaik;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class KeyBindingsTest {

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

public KeyBindingsTest() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                ex.printStackTrace();
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

public class TestPane extends JPanel {

    private JLabel state;

    /**
     * 
     */
    public TestPane() {
        setLayout(new GridBagLayout());
        state = new JLabel("Nothing here");
        add(state);

        InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
        im.put(KeyStroke.getKeyStroke("ctrl 7"), "ctrl+7");
        im.put(KeyStroke.getKeyStroke("ctrl shift 7"),  "Ctrl+3");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");

        ActionMap am = getActionMap();
        am.put("Ctrl+1", new MessageAction("Ctrl+1"));
        am.put("Ctrl+2", new MessageAction("Ctrl+2"));
        am.put("Ctrl+3", new MessageAction("Ctrl+3"));
        am.put("ctrl+7", new MessageAction("ctrl+7"));
        am.put("Ctrl+4", new MessageAction("Ctrl+4"));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

    public class MessageAction extends AbstractAction {

        private String message;

        public MessageAction(String message) {
            this.message = message;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            state.setText(message);
        }

    }

}

}

请帮忙。

4

1 回答 1

1

另一种解决方案:在你的 TestPane 类中试试这个,你可以捕捉到事件本身:

getRootPane().getContentPane().getToolkit().addAWTEventListener(new AWTEventListener() {

        public void eventDispatched(final AWTEvent event) {
            if (event.getID() == KeyEvent.KEY_PRESSED) {
                final KeyEvent keyEvent = (KeyEvent) event;
                switch (keyEvent.getKeyCode()) {
                    case KeyEvent.VK_NUMPAD7:
                        if(keyEvent.isControlDown() && keyEvent.isShiftDown()) {
                            //do Stuff
                        }
                        break;
                }
            }
        }

    }, AWTEvent.KEY_EVENT_MASK);

我不确定您的事件地图有什么问题。

于 2016-04-29T09:35:12.427 回答