• 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

java.awt.event.ActionEvent.CTRL_MASK what is it?

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please tell me what the CTRL_MASK does , i read from the docs that its a control modifier .. and when i try to print it out using  System.out.println(ActionEvent.CTRL_MASK) while holding ctrl key , it results in 2 , and when not pressing control key , its still 2.

I have written a program ,,, here's the code , line 93 shows it

 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what key did you press (apart from the Ctrl key) to cause that ActionEvent?
 
Gourav Das
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:And what key did you press (apart from the Ctrl key) to cause that ActionEvent?




every other key , 'x','y','a', etc.. , still every time the result is 2 which is printed out in the console.... the key is pressed when i click on the "Button Exit"... :(

help-5.PNG
[Thumbnail for help-5.PNG]
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So just to confirm -- you are pressing the Ctrl key and some alphabetic key and clicking with the mouse on the button? Did you try finding out what alphabetic key was pressed at the same time?

I've never tried that and I wouldn't have expected to get key-press information when you click on a button. Apparently what I expected was correct.

I would expect that if you wrote a KeyListener then you would get the information you're looking for. But in real life, users don't expect clicking on a button in a GUI to be affected by what keys you happen to be pressing at the time.
 
Gourav Das
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:So just to confirm -- you are pressing the Ctrl key and some alphabetic key and clicking with the mouse on the button? Did you try finding out what alphabetic key was pressed at the same time?

I've never tried that and I wouldn't have expected to get key-press information when you click on a button. Apparently what I expected was correct.

I would expect that if you wrote a KeyListener then you would get the information you're looking for. But in real life, users don't expect clicking on a button in a GUI to be affected by what keys you happen to be pressing at the time.




Actually  , i just was playing around with the CTRL_MASK , after reading the docs ... Can you please tell me what it is used for?

Herer are the several fields in ActionEvent Class..
help-6.PNG
[Thumbnail for help-6.PNG]
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

while holding ctrl key , it results in 2 , and when not pressing control key , its still 2.


Yes, because you are just printing a static value from the ActionEvent class.

You need to invoke the getModifiers() method of the ActionEvent to see if any modifier key was used when the button was clicked.

Here is a basic ActionListener that displays the modifier keys that were pressed when the button was clicked:



 
Gourav Das
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:. . .



Thank You
 
Paul Clapham
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:You need to invoke the getModifiers() method of the ActionEvent to see if any modifier key was used when the button was clicked.



Well, that part I should have known. But I'll be damned, it actually works with button clicks.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The term "mask" refers to a bit mask, which is why Rob wrote (modifiers & mask) == mask -- that is, take the modifiers value and mask it (perform bitwise & operation) with the bits in the mask variable to see if the bits that are set in mask are all set in modifiers

If modifiers == 7, that's 0111 in binary. A mask of 2 is 0010 in binary.

  0111     modifiers
& 0010     mask
------
  0010  == mask

If you look at the values of the mask constants, you'll see that each mask constant represents a bit position that is set. That is, mask values are 1, 2, 4, 8 (binary 0001, 0010, 0100, 1000, respectively). If you wanted to check if both the CTRL and SHIFT keys were pressed, you'd write checkMask(modifiers, CTRL_MASK + SHIFT_MASK). In general, you can check for any combination of keys pressed by using the sum of their respective mask constants.

I'd probably rename that method to read like this instead: isKeyPressed(modifiers, CTRL_MASK + SHIFT_MASK) for better semantics.
reply
    Bookmark Topic Watch Topic
  • New Topic