permaculture playing cards
The moose likes Java in General and the fly likes Shortcut Keys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Shortcut Keys" Watch "Shortcut Keys" New topic

Shortcut Keys

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Can anyone tell me what class to use to create shortcut keys?
Example: If I have a button named "Go", and I want to create a shortcut with Alt G. How do I do this? Thanks in advance!!!

Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

If you are doing this in Swing it is built in... just do:

In the code above it is actually the g key and not Alt-G that "presses" the button... but this is the easiest way to do it.
If, on the other hand, you have your heart set on Alt-G, or you are using the AWT components, you will need to define a KeyListener for your components and test for an Alt-G combination. (Probably with a case statement that goes through all the keys you use and calls a method that is also called by the buttons ActionListener...)

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Thanks Nathan. I will try it!
It is sorta covered in the JavaRanch Style Guide.
subject: Shortcut Keys
It's not a secret anymore!