简体   繁体   中英

Keylistener not responding to numlock numbers

i'm making a keylistener that listens to ctrl-1 and ctrl-2.

Im making a quiz for teams. Team 1 should press ctrl-1 if they want to answer. Team 2 should press ctrl-2 if they want to answer.

The reason i chose for ctrl is because there are 2 control keys. So 2 teams can play against eachother on 1 keyboard.

I want team 1 to use the left control and the numbers under F1-F12. And team 2 to use the right control and the numbers on the numlock.

My code registrates the triggers of team 1 but not from team 2. Here is my code :

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

        }

EDIT : I just did it with key bindings, gives the same problem, here is the code.

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

Thank you!

Firstly, I would highly recommend using the key bindings API .

Second KeyEvent.VK_1 is not the same event that is raised for numpad+1, this is triggered by KeyEvent.VK_NUMPAD1 instead, it's a different key event, just like the function keys are raise KeyEvent.VK_F1 to 12 events.

For example...

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

        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM