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 byKeyEvent.VK_NUMPAD1
instead, it's a different key event, just like the function keys are raiseKeyEvent.VK_F1
to12
events.For example...