Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ALT_MASK and CTRL_MASK

 
G Rachar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to make a menu with a keyboard accelerator key combination of Ctrl+Alt+A. the following line will make a keyboard shortcut of Ctrl+A:

jmiAdd.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));

on the menu will be the light blue letters "Ctrl-A" next to the menu entry, plus you can press Ctrl-A at any time to execute the instruction.

by changing CTRL_MASK to ALT_MASK, you change it to "Alt-A".

what i want is to require Ctrl and Alt and A to be pressed simultaneously to execute the instruction. my first guess was CTRL_ALT_MASK, but that didn't work.

i could kluge the code testing for control and alt pressed at the same time, but i want the proper accelerator note "Ctrl+Alt-A" to show up on the menu beside the menu entry.

have been to several websites on keystroke handling but none talk about combinations.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing...
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that CTRL_MASK and ALT_MASK are just int's and I seem to remember adding ints together to make this work:



Let me know if that worked.
 
R.Joe
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello G Rachar,
Sir i have seen your problem and also Mr. Gregg Bolinger Solution i.e.

jmiAdd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK+ALT_MASK));

but that is not working and it was wrong u can use either this

jmiAdd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK + ActionEvent.ALT_MASK));

or
jmiAdd.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, 10));

10 because 10 is a combination of 8 + 2

8 is ALT

2 is CTRL
now it's works ,have a look, and all the best
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi is correct. I had a typo in my code. I left out the ActionEvent part just before ALT_MASK.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic