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

I need to see if the CTRL key is pressed while processing a mouse event. I tried using KeyListener, but trying to use the mouse event and the key event together is a problem due to focus issues.

What I'm trying to accomplish is to select multiple objects using the CTRL key, as on Windows.

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

Can you do it in Java?

Thanks.

+6
source share
2 answers

The MouseEvent extends from InputEvent, and I think you can still get the modifiers of this object using getModifiers() to see if the ctrl key is pressed. I have not tested this yet.

+10
source

Use getModifiers() to detect a pressed key.

, eg:

 if ((event.getModifiers() & ActionEvent.CTRL_MASK) ==ActionEvent.CTRL_MASK) { System.out.println("CTRL KEY PRESSED"); } 
+8
source

Source: https://habr.com/ru/post/923406/


All Articles