2

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    please shouldn be KeyEvent.VK_1 && KeyEvent.CTRL_MASK ??? EDIT if (code == KeyEvent.VK_1 && modifiers == KeyEvent.CTRL_MASK) { – mKorbel May 13 '14 at 12:12
  • should be so easilly by using KeyBindings instaead of KeyListener – mKorbel May 13 '14 at 12:15
  • Did you test if either 1 or 2 alone (without CTRL) are working? – Angelo Fuchs May 13 '14 at 12:15
  • How can i do with Keybindings, dont really understand the tutorial... – user3284809 May 13 '14 at 12:17
  • No didn't test it without the ctrl – user3284809 May 13 '14 at 12:18
  • @user3284809 1. you can to search here, incl. working code examples, 2. quite offen, two-three times daily in answers here, 3. most important is question how did you set the focus to something, because JComponents must be focusable for receiving a KeyEnvents from KeyListener – mKorbel May 13 '14 at 12:37
  • Try using getInputMap(WHEN_IN_FOCUSED_WINDOW) – MadProgrammer May 13 '14 at 12:38
  • 1
    I put this in the constructor of the JPanel : setFocusable(true); – user3284809 May 13 '14 at 12:40
  • @user3284809 Then please do test it I would assume that you don't receive numpad input (for whatever reason) OR that the second CTRL Key is broken (or sends the wrong keycode). – Angelo Fuchs May 13 '14 at 12:43
  • So i just tested the numpad without ctrl, that works. I also tested the 2nd ctrl with the other non numpad numbers so that works aswell. Im wondering if ctrl + numpad ever works? Even with the left ctrl it doesn't work with numpad. – user3284809 May 13 '14 at 12:46

1 Answers1

3

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

        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366