slysy Posted August 22, 2005 Share Posted August 22, 2005 Does anyone know how to do this? e.g. user presses F1 and it will activate a JButton? I have tried setMnemonic() but this means you have to press alt and the F-Key instead of just the F-Key on its own. Any help would be much appreciated :cool: Link to comment Share on other sites More sharing options...
0 MrStaticVoid Posted August 22, 2005 Share Posted August 22, 2005 It's been a while since I've done Swing, but I suspect you'll want to add some sort of key listener to the root window to listen for certain key presses. Upon the correct key press perform the correct handling code. Link to comment Share on other sites More sharing options...
0 slysy Posted August 22, 2005 Author Share Posted August 22, 2005 It's been a while since I've done Swing, but I suspect you'll want to add some sort of key listener to the root window to listen for certain key presses. Upon the correct key press perform the correct handling code. 586414086[/snapback] Thanks for your suggestion. I have tried using keyReleased on the JFrame but the event never seems to fire. Any other ideas? Link to comment Share on other sites More sharing options...
0 STV Posted August 22, 2005 Share Posted August 22, 2005 (edited) how about this: import javax.swing.*; import java.awt.event.*; class TestFrame extends JFrame implements KeyListener ?{ public TestFrame() { : //other stuff here this.addKeyListener(this); //need to make the Form listen for keys being typed, pressed, and released. : //other stuff here }//end of default constructor ?public void keyPressed(KeyEvent e) ? ?{ ? ?switch(KeyEvent.getKeyText(e.getKeyCode())) ? ? ?{ ? ? ?case 'F': case 'f': /*do stuff here */ break; ? ? ?: ? ? ?: //add more cases as needed ? ? ?}//end of switch ? ?}//end of keyPressed() ?public void keyReleased(KeyEvent e) ? ?{ ? ?}//end of keyReleased() public void keyTyped(KeyEvent e) ? ? ?{ ? ?}//end of keyTyped() ?}//end of class hope this helps, STV Edited August 22, 2005 by STV Link to comment Share on other sites More sharing options...
0 Wheazel Posted August 22, 2005 Share Posted August 22, 2005 Try adding the listener to the rootpane Link to comment Share on other sites More sharing options...
0 slysy Posted August 22, 2005 Author Share Posted August 22, 2005 I got is working guys, thanks for your help. In the end I found a custom class in the source tree that does what I want. Sorry to anyone else wanting to solve this problem but I am unable to post this class, I am sure you will understand :( Link to comment Share on other sites More sharing options...
0 STV Posted August 22, 2005 Share Posted August 22, 2005 well, the point is to solve the problem. you solved it by searching for a solution. good job. STV Link to comment Share on other sites More sharing options...
Question
slysy
Does anyone know how to do this? e.g. user presses F1 and it will activate a JButton? I have tried setMnemonic() but this means you have to press alt and the F-Key instead of just the F-Key on its own. Any help would be much appreciated :cool:
Link to comment
Share on other sites
6 answers to this question
Recommended Posts