Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

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

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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: 25452
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 25452
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3158
29
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator

Rob Camick wrote:. . .



Thank You
 
Paul Clapham
Marshal
Posts: 25452
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 15525
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
That is a really big piece of pie for such a tiny ad:
Learn to write code others love to read, one example at a time!
java.by-comparison.com
    Bookmark Topic Watch Topic
  • New Topic