Results 1 to 8 of 8
- 01-20-2012, 10:48 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
How to add hotkey (F12) to a button?
Hey there all. I need to find out how to add a hotkey to a button (F12 key) so that it activates and deactivates when they hotkey is pressed or released.
The script is:
Thanks for any and all help. It is really appreciated.Java Code:} JButton button = new JButton("Press This Button to Talk"); button.addMouseListener(this); add(button); setSize(new Dimension(300, 100)); }
-
Re: How to add hotkey (F12) to a button?
Check out the AbstractButton API and look up a method that contains the word mnemonic in it. That will help you assign an alt+keystroke combination to a JButton. If you want to associate a non-alt-keystroke combination to a JButton, you'll want to use Key Bindings, and the Java tutorials will show you how to do this.
- 01-20-2012, 10:56 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: How to add hotkey (F12) to a button?
The Swing tutorial has a section on: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
Last edited by camickr; 01-20-2012 at 10:59 PM.
- 01-20-2012, 11:23 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
Re: How to add hotkey (F12) to a button?
Thanks for both of your responses. As per suggestions, would this work?
I just wanted a shortcut key that has the same effect as pressing the button with the mouse. The button is meant to listen for voice while the mouse button is pressed down on it and to stop listening when the mouse is released. This is what I wanted the hotkey to do as well.Java Code:} JButton button = new JButton("Press This Button to Talk"); button.addMouseListener(this); add(button); setSize(new Dimension(300, 100)); component.getInputMap().put(KeyStroke.getKeyStroke("F12"), "pressed"); component.getInputMap().put(KeyStroke.getKeyStroke("released F12"), "released"); component.getActionMap().put("pressed", pressedAction); component.getActionMap().put("released", releasedAction); }
-
Re: How to add hotkey (F12) to a button?
- 01-20-2012, 11:36 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 6
- Rep Power
- 0
Re: How to add hotkey (F12) to a button?
I was asking more if the syntax seemed correct... with that said I'm trying it now :)We're not your compiler. Run the code, test the code, report back if you have errors or it doesn't work.
-
Re: How to add hotkey (F12) to a button?
Well, you shouldn't be adding a MouseListener to a JButton but rather an ActionListener -- please have a look at the tutorials regarding how to use buttons. Also, where did you get the "Released F12" String from? -- I've never seen that one before, and highly suspect that it won't work.
- 01-21-2012, 04:49 AM #8
Similar Threads
-
How can I to add a mouse event to a button that it'll edit itself and other button
By mad72584 in forum New To JavaReplies: 7Last Post: 08-06-2011, 03:39 AM -
Make a button class that uses your button image.
By eLancaster in forum New To JavaReplies: 1Last Post: 04-26-2011, 11:32 AM -
Adding a sqrt button and percentage button to a calculator
By Josie_Taylor in forum New To JavaReplies: 4Last Post: 03-14-2011, 01:16 AM -
outside hotkey
By Dennis in forum Advanced JavaReplies: 4Last Post: 06-18-2010, 07:37 PM -
How to add hotkey(Ctrl+VK) to a button?
By rpwtdj in forum Advanced JavaReplies: 5Last Post: 01-10-2008, 02:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks