Linking keyboard keys to buttons in JAVA GUI
843806Apr 23 2008 — edited Apr 23 2008Hi all,
I'm new to this forum and failry new to JAVA, so forgive me if Im asking stupid questions.
I have a Java GUI that acts as a user interface for controlling a device. I have 5 buttons: "up", "down", "left", "right", and "Fireworks".
My goal is to link the the keyboard keys "up arrow", "down arrow", ... , "F" to these buttons such that the buttons are virually pressed when these keys are hit on the keyboard.
I've already searched a bit and have ran into the following problems:
-Using mnemonics seems to require the use of the Alt key, which is not desirable for what I am doing.
-This would also require that a certain object be selected - only then would the keyboard key be listened to.
Note: What i was hoping for is that it would be possible to link the keys to the buttons, such that when the keys are pressed, the buttons are virtually pressed, meaning that any code under the method buttonMousePressed(MouseEvent evt) would be executed directly, by simply pressing the key associated with that button. Then, it wouldn't be necessary to have duplicate code.. under buttonMousePressed method and specificKeyPressed method.
Is this possible?
Can anyone provide a simple example for me?
Thanks so much in advance!