• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to check for ALT down when pressing Button2

 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just use the isAltDown() method of the MouseEvent.
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Works fine for me using JDK7 on Windows 7.

Post your SSCCE that demonstrates the problem.
 
Author
Posts: 986
3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic