Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyPressed event in java

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
}
like image 333
CodeLover Avatar asked Oct 24 '12 03:10

CodeLover


People also ask

What is ADD KeyListener in Java?

The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent. The KeyListener interface is found in java. awt. event package, and it has three methods.

What does keyTyped do in Java?

The KeyTyped() listener method is called when a character is typed, but is not useful for virtual keys (arrow keys, function keys, etc). Modifier key (shift, control, etc) status (up/down) can be tested with method calls in the listener. These methods are called whenever any key is pressed or released.

How do you check if a specific key is pressed in Java?

Use KeyEvent. getKeyChar() and KeyEvent. getKeyCode() to find out which key the user pressed.

What is keyCode in Java?

The variable keyCode is used to detect special keys such as the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT.


3 Answers

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.

like image 107
MadProgrammer Avatar answered Oct 23 '22 05:10

MadProgrammer


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.

like image 4
Jake Stoeffler Avatar answered Oct 23 '22 04:10

Jake Stoeffler


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?

like image 1
Romski Avatar answered Oct 23 '22 03:10

Romski