简体   繁体   中英

How to get the key pressed with an awt mouse event

I have a Swing application and I need to make a specific action if an alphanumeric key is pressed during a mouse drag action. I successfuly managed the drag action but I can't get the key pressed. It seems that only the modifiers (alt, ctrl, shift) are available in a mouse event?

Is there aa way to know if for example the W key is pressed in a java.awt.event.MouseAdapter method (without adding a keyListener) ?

Is there aa way to know if for example the W key is pressed in a java.awt.event.MouseAdapter method (without adding a keyListener) ?

No, you need to use a KeyListener.

And you also need to reverse your logic. That is when you handle the KeyEvent you need to check if the mouse is also being used by looking at the "modifiers" of the KeyEvent. Read the API for more information on the getModifiersEx() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM