I have a JPanel that I need to check for the control being pressed down so that the user can select multiple things on screen, i had the issues of using a key listener so after research i found that i was supposed to use key bindings, and i finally got it to work for pressing control, but i cant get it to work for releasing control '''
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,
InputEvent.CTRL_DOWN_MASK), "press");
getActionMap().put("press", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
controlPressed = true;
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK
,InputEvent.CTRL_DOWN_MASK,true), "release");
getActionMap().put("release", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("release");
controlPressed = false;
}
});
'''
so pressing ctrl works but releasing does not, any ideas?
Update, I found what I think is the best solution for my problem, the mouse event stores weather or not control is pressed down so there was no need to do any magic with keysListeners or binding keys. here is the line of code if anyone needs it
'''
public void mousePressed(MouseEvent e){
e.isControlDown();
}
'''