KeyPressed event in java

51,404

Solution 1

Depending on where you want to trap the "enter" key, you could use an ActionListener (on such components such as text components or buttons) or attach a key binding to you component

public class MyPanel extends JPanel {

    public MyPanel() {

        InputMap im = getInputMap(WHEN_FOCUSED);
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "onEnter");

        am.put("onEnter", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Enter pressed
            }
        });

    }

}

This will rely on the component being focused.

Solution 2

One way is to implement the KeyListener interface and its key event methods. For example,

public class MyClass  implements KeyListener {
    public void keyTyped(KeyEvent e) {
        // Invoked when a key has been typed.
    }

    public void keyPressed(KeyEvent e) {
        // Invoked when a key has been pressed.
        if (e.getKeyCode() == KeyEvent.VK_ENTER && yourOtherCondition) {
            myMethod();
        }
    }

    public void keyReleased(KeyEvent e) {
        // Invoked when a key has been released.
    }
}

Then add this listener with

myComponent.addKeyListener(new MyClass());

Or if you prefer,

myComponent.addKeyListener(new KeyListener() {
    public void keyPressed(KeyEvent e) { /* ... */ }

    public void keyReleased(KeyEvent e) { /* ... */ }

    public void keyTyped(KeyEvent e) { /* ... */ }
});

See this for more details.

Solution 3

Caveat - It's been a while since I did desktop applications, but the java.awt.Component class has an addKeyListener() method which you can use to register a class that implements KeyListener - is this what you are looking for?

Share:
51,404

Related videos on Youtube

CodeLover
Author by

CodeLover

Updated on October 15, 2020

Comments

  • CodeLover
    CodeLover over 3 years

    I have just created a java tic-tac-toe game i would like to figure out how to run a method once the enter key is pressed during a certain condition an example is below...

    if(/*condition is met*/){
         //keyListener
    }
    
  • CodeLover
    CodeLover over 11 years
    but what would i set the keylistener on ?
  • CodeLover
    CodeLover over 11 years
    i get an error on the WHEN_FOCUSED, and the getActionMap(); method call
  • CodeLover
    CodeLover over 11 years
    they both say cannot find symbol
  • MadProgrammer
    MadProgrammer over 11 years
    You need to apply key bindings to Swing components (that extend from JComponent). A little bit more context to your question would make it easier to guide you
  • Romski
    Romski over 11 years
    The answer you accepted uses a JPanel. A Jpanel is a type of Component, and therefore inherits the addKeyListener() method. I guess the question would be, what component(s) do you need to listen for key events and add it to that. By the way, I'm not suggesting my answer was any better!