2

我正在制作一个监听 ctrl-1 和 ctrl-2 的 keylistener。

我正在为团队做一个测验。如果他们想回答,团队 1 应该按 ctrl-1。团队 2 如果他们想回答,应该按 ctrl-2。

我选择 ctrl 的原因是因为有 2 个控制键。因此 2 支球队可以在 1 个键盘上互相对抗。

我希望团队 1 使用左侧控件和 F1-F12 下的数字。团队 2 使用正确的控件和数字锁上的数字。

我的代码注册了团队 1 的触发器,但没有注册团队 2。这是我的代码:

        public void keyPressed(KeyEvent e) {
            if((QuizController)getController() != null){
                if(e.getKeyCode () == KeyEvent.VK_1){
                    if((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)
                        System.out.println("Team 1");
                }
                if(e.getKeyCode () == KeyEvent.VK_2){
                    if((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)
                        System.out.println("Team 2");
                }
            }

        }

编辑:我只是用键绑定做的,给出了同样的问题,这里是代码。

AbstractAction team1 = new AbstractAction() {
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("Team 1");

        }
    };

AbstractAction team2 = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("Team 2");

        }
    };

    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_1, java.awt.event.InputEvent.CTRL_DOWN_MASK),"actionMap1");
    getActionMap().put("actionMap1", team1);

    getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_2, java.awt.event.InputEvent.CTRL_DOWN_MASK),"actionMap2");
    getActionMap().put("actionMap2", team2);

谢谢!

4

1 回答 1

3

首先,我强烈推荐使用key bindings API

第二个KeyEvent.VK_1不是为 numpad+1 引发的KeyEvent.VK_NUMPAD1事件,而是由不同的键事件触发,就像功能键引发KeyEvent.VK_F1事件一样12

例如...

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");

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

        @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);
            }

        }

    }

}
于 2014-05-13T12:53:45.363 回答