Results 1 to 8 of 8
  1. #1
    steven.smith is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Question 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:
    Java Code:
        
    
    }
        JButton button = new JButton("Press This Button to Talk");
    
        button.addMouseListener(this);
    
        add(button);
    
        setSize(new Dimension(300, 100));
    
      }
    Thanks for any and all help. It is really appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default Re: How to add hotkey (F12) to a button?

    Last edited by camickr; 01-20-2012 at 10:59 PM.

  4. #4
    steven.smith is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Question Re: How to add hotkey (F12) to a button?

    Thanks for both of your responses. As per suggestions, would this work?

    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);
      }
    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to add hotkey (F12) to a button?

    Quote Originally Posted by steven.smith View Post
    Thanks for both of your responses. As per suggestions, would this work?
    We're not your compiler. Run the code, test the code, report back if you have errors or it doesn't work.

  6. #6
    steven.smith is offline Member
    Join Date
    Jan 2012
    Posts
    6
    Rep Power
    0

    Default Re: How to add hotkey (F12) to a button?

    We're not your compiler. Run the code, test the code, report back if you have errors or it doesn't work.
    I was asking more if the syntax seemed correct... with that said I'm trying it now :)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to add hotkey (F12) to a button?

    Moved from 'Advanced Java'

    Also, this is in no way an advanced topic. If you have a non-GUI question of this level, please post in 'New to Java'

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 7
    Last Post: 08-06-2011, 03:39 AM
  2. Make a button class that uses your button image.
    By eLancaster in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:32 AM
  3. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 01:16 AM
  4. outside hotkey
    By Dennis in forum Advanced Java
    Replies: 4
    Last Post: 06-18-2010, 07:37 PM
  5. How to add hotkey(Ctrl+VK) to a button?
    By rpwtdj in forum Advanced Java
    Replies: 5
    Last Post: 01-10-2008, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •