[java] KeyListener, keyPressed versus keyTyped

I have a JFrame (well, a class which extends JFrame) and I want to do an action when I press the F5 key. So, I made the class implement KeyListener. And with that, came three methods, keyPressed, keyReleased, and keyTyped.

Which of these methods should I use to listen for F5 being pressed? keyPressed or keyTyped? I currently have the following, however it does not print anything when I press F5.

public void keyPressed(KeyEvent e) {
    if(e.getKeyCode() == KeyEvent.VK_F5)
        System.out.println("F5 pressed");
}

public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

This question is related to java swing jframe key-bindings keylistener

The answer is


You should use keyPressed if you want an immediate effect, and keyReleased if you want the effect after you release the key. You cannot use keyTyped because F5 is not a character. keyTyped is activated only when an character is pressed.


private String message;
private ScreenManager s;


//Here is an example of code to add the keyListener() as suggested; modify 
public void init(){
Window w = s.getFullScreenWindow();
w.addKeyListener(this);

public void keyPressed(KeyEvent e){
    int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_F5)
            message = "Pressed: " + KeyEvent.getKeyText(keyCode);
}

Neither. You should NOT use a KeyLIstener.

Swing was designed to be used with Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to jframe

JFrame background image JFrame: How to disable window resizing? Adding image to JFrame How to make a JFrame button open another JFrame class in Netbeans? How to layout multiple panels on a jFrame? (java) How to add text to JFrame? JFrame.dispose() vs System.exit() JPanel vs JFrame in Java Adding JPanel to JFrame Limiting the number of characters in a JTextField

Examples related to key-bindings

How can I listen for keypress event on the whole page? How do I bind the enter key to a function in tkinter? How to select all instances of a variable and edit variable name in Sublime KeyListener, keyPressed versus keyTyped Binding arrow keys in JS/jQuery

Examples related to keylistener

Keylistener in Javascript Allowing the "Enter" key to press the submit button, as opposed to only using MouseClick Key Listeners in python? How to use KeyListener KeyListener, keyPressed versus keyTyped Place cursor at the end of text in EditText Unresponsive KeyListener for JFrame