4

I have to run a method manually when pressing the key Alt+H

if("The key pressed==(Alt+H)"){
    callMethod();
}

public void callMethod(){
    //Some codes here
}

How I can actually do this in Java. Please give me a simple way to do this.

Bashir
  • 2,057
  • 5
  • 19
  • 44
Yithirash
  • 377
  • 3
  • 6
  • 18

3 Answers3

7

It's worth reading here about Oracle Tutorial - Enabling Keyboard Operation where it is explained in details along with sample.

Read more about on Oracle Tutorial - How to Use Key Bindings

Some example directly from the above tutorial:

//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
                     KeyEvent.VK_H);

//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_H);

//Setting the accelerator:
menuItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_H, ActionEvent.ALT_MASK));

Read more here Oracle Tutorial - How to Use Buttons, Check Boxes, and Radio Buttons

Sample code: (Alt-H would click the Middle button)

JButton b2 = new JButton("Middle button", middleButtonIcon);
b2.setMnemonic(KeyEvent.VK_H);
Braj
  • 46,415
  • 5
  • 60
  • 76
2

If using menus then you can use setMnemonic(), see How to Use Menus for examples. Another option is to use Key Bindings. For example:

import java.awt.event.*;
import javax.swing.*;

public class TestKeys {
    private static void createAndShowGUI() {
        final JFrame frame = new JFrame("Keys");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.ALT_MASK);

        Action testAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                 JOptionPane.showMessageDialog(frame, "Alt-H pressed");
            }
        };

        frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "TestAction");
        frame.getRootPane().getActionMap().put("TestAction", testAction);       

        JLabel label = new JLabel("Hit Alt-H");
        label.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        frame.add(label);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
0

Best method is to use setMnemonic() because its the the simplest.

check this article for more http://www.herongyang.com/Swing/JMenuBar-Set-Keyboard-Mnemonics-on-Menu-Items.html

private JMenu getColorMenu() {
  JMenu myMenu = new JMenu("Color");
  ButtonGroup myGroup = new ButtonGroup();

  JRadioButtonMenuItem myItem = new JRadioButtonMenuItem("Red");
  myItem.setSelected(true);
  myItem.setMnemonic(KeyEvent.VK_R);
  myItem.addActionListener(this);
  myItem.addMenuKeyListener(this);
  myGroup.add(myItem);
  myMenu.add(myItem);

  myItem = new JRadioButtonMenuItem("Green");
  myItem.setMnemonic(KeyEvent.VK_G);
  myItem.addActionListener(this);
  myItem.addMenuKeyListener(this);
  myGroup.add(myItem);
  myMenu.add(myItem);

  myItem = new JRadioButtonMenuItem("Blue");
  myItem.setMnemonic(KeyEvent.VK_B);
  myItem.addActionListener(this);
  myItem.addMenuKeyListener(this);
  myGroup.add(myItem);
  myMenu.add(myItem);

  return myMenu;