Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to check for ALT down when pressing Button2  RSS feed

 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my MouseListener mousePressed() method, when I press button 2 I cannot tell whether the ALT button is pressed because both InputEvent.BUTTON2_MASK and InputEvent.ALT_MASK are 8. Thus is always true whether the alt key is pressed or not. Checking for ALT_DOWN_MASK does not work because getModifiers returns 8 and ALT_DOWN_MASK is 512. (Using Java8 on a PC). Are there any solutions?
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use the isAltDown() method of the MouseEvent.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
event.isAltDown() always returns`true` when button2 is pressed, regardless of whether or not the alt key is down.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Works fine for me using JDK7 on Windows 7.

Post your SSCCE that demonstrates the problem.
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you may be stuck. Systems with single-button mice are supposed to be able to use alt-click to simulate mouse button2. Essentially, an alt-click is a button2 click and vice versa. (Same deal with meta-click and mouse button3.)

I don't think there is any way in the AWT/Swing APIs to distinguish between an alt-click and a button2 click.

All that InputEvent.isAltDown() does is test the ALT_MASK modifiers bit, so will behave the same as testing that bit yourself. (InputEvent is the parent class of MouseEvent.)
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Running on Windows7 using either Java7 or Java8, when I click in the panel it writes "true" to the screen regardless of whether ALT is down or not.

From Brian Cole's explanation, I guess I will have to live with it and uses some other combination of shift/ctrl to do what I want (this is in a plotting package where different key/mouse actions do different things, such as zoom, unzoom, select, edit points, drag annotations, drag legend, highlight lines, etc.)
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, sorry, I wasn't paying attention. I only tested my right/left buttons, not the middle. I do have the same problem when I test the middle.

Glad Brian know what he's talking about
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!