Detecting if a key (CTRL) is currently pressed without a KeyEvent in Java

StackOverflow https://stackoverflow.com/questions/12059669

  •  27-06-2021
  •  | 
  •  

سؤال

I need to be able to see if the CTRL key is currently being pressed during the processing of a mouse event. I have tried to use the KeyListener, but trying to use the mouse event and key event together is proving to be an issue because of focus issues.

What I am essentially trying to accomplish is selecting multiple objects using the CTRL key like in Windows.

It would be much easier if, while in my mouse event, I could just check the status of the CTRL key...

Can you do that in Java?

Thanks.

هل كانت مفيدة؟

المحلول

MouseEvent extends from InputEvent, and I think that you can still get the modifiers from this object via getModifiers() to see if a ctrl key has been pressed. I've not tested this yet though.

نصائح أخرى

Use getModifiers() to detect the key pressed..

eg:

if ((event.getModifiers() & ActionEvent.CTRL_MASK) ==ActionEvent.CTRL_MASK) {
System.out.println("CTRL KEY PRESSED");
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top