aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes ALT_MASK   and   CTRL_MASK Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ALT_MASK   and   CTRL_MASK" Watch "ALT_MASK   and   CTRL_MASK" New topic
Author

ALT_MASK and CTRL_MASK

G Rachar
Greenhorn

Joined: Jun 18, 2004
Posts: 1
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

Moving to Swing...


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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

Joined: Jun 16, 2004
Posts: 31
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ravi is correct. I had a typo in my code. I left out the ActionEvent part just before ALT_MASK.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ALT_MASK and CTRL_MASK