Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shortcut Keys

 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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!!!

Barry
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry,
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...)
HTH,
-Nate
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nathan. I will try it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic