You can use isControlDown() method:

switch (e.getKeyCode())
{
        case KeyEvent.VK_A :
            if(e.isControlDown())
               System.out.println("A and Ctrl are pressed.");
            else
                System.out.println("Only A is pressed");
            break;
        ...
}
Answer from Juvanis on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › awt › event › KeyEvent.html
KeyEvent (Java Platform SE 7 )
... the integer code for an actual ... key. ... Returns the character associated with the key in this event. For example, the KEY_TYPED event for shift + "a" returns the value for "A"....
🌐
Bureau of Economic Geology
beg.utexas.edu › lmod › agi.servlet › doc › detail › java › awt › event › KeyEvent.html
java.awt.event Class KeyEvent
The event is passed to every KeyListener or KeyAdapter object which registered to receive such events using the component's addKeyListener method. (KeyAdapter objects implement the KeyListener interface.) Each such listener object gets this KeyEvent when the event occurs.
🌐
Tabnine
tabnine.com › home page › code › java › java.awt.event.keyevent
java.awt.event.KeyEvent java code examples | Tabnine
For example, for Japanese Windows keyboards, VK_ALL_CANDIDATES is returned instead of VK_CONVERT with the ALT modifier. WARNING: Aside from those keys that are defined by the Java language (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ constants.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.desktop › java › awt › event › KeyEvent.html
KeyEvent (Java SE 21 & JDK 21)
January 20, 2026 - A constant indicating that the key pressed or released is in the right key location (there is more than one possible location for this key). Example: the right shift key. ... A constant indicating that the key event originated on the numeric keypad or with a virtual key corresponding to the ...
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.desktop › java › awt › event › KeyEvent.html
KeyEvent (Java SE 17 & JDK 17)
January 20, 2026 - A constant indicating that the key pressed or released is in the right key location (there is more than one possible location for this key). Example: the right shift key. ... A constant indicating that the key event originated on the numeric keypad or with a virtual key corresponding to the ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › awt › event › KeyEvent.html
KeyEvent (Java Platform SE 8 )
4 weeks ago - ... the integer code for an actual ... key. ... Returns the character associated with the key in this event. For example, the KEY_TYPED event for shift + "a" returns the value for "A"....
🌐
TutorialsPoint
tutorialspoint.com › awt › awt_key_event.htm
AWT KeyEvent Class
Following is the declaration for java.awt.event.KeyEvent class: public class KeyEvent extends InputEvent · Following are the fields for java.awt.InputEvent class: static char CHAR_UNDEFINED --KEY_PRESSED and KEY_RELEASED events which do not map to a valid Unicode character use this for the keyChar value.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › uiswing › events › keylistener.html
How to Write a Key Listener (The Java™ Tutorials > Creating a GUI With Swing > Writing Event Listeners)
The following example demonstrates key events. It consists of a text field that you can type into, followed by a text area that displays a message every time the text field fires a key event. A button at the bottom of the window lets you clear both the text field and text area.
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.desktop › share › classes › java › awt › event › KeyEvent.java
jdk/src/java.desktop/share/classes/java/awt/event/KeyEvent.java at master · openjdk/jdk
* @see <a href="https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html">Tutorial: Writing a Key Listener</a> ... /* Japanese PC 106 keyboard with special Windows driver - eisuu + Control; Japanese Solaris keyboard: kana */ public static final int VK_KANA_LOCK = 0x0106; ... * @deprecated as of JDK1.1; use {@link #KeyEvent(Component, int, long, int, int, char)} instead
Author   openjdk
🌐
Tabnine
tabnine.com › home page › code › java › java.awt.event.keyevent
java.awt.event.KeyEvent.getKeyCode java code examples | Tabnine
@Override public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_ENTER){ System.out.println("Hello"); JOptionPane.showMessageDialog(null , "You've Submitted the name " + nameInput.getText()); } }
🌐
javaspring
javaspring.net › blog › java-keyevent
Mastering Java KeyEvents: A Comprehensive Guide — javaspring.net
When handling KeyEvents, avoid performing long-running or blocking operations in the event handling methods. This can cause the application to become unresponsive. If you need to perform a long-running operation, consider using a separate thread. import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JLabel; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NonBlockingKeyEventExample extends JFrame implements KeyListener { private JLabel label; private ExecutorService executorService = Exec
🌐
Java
download.java.net › java › early_access › loom › docs › api › java.desktop › java › awt › event › KeyEvent.html
KeyEvent (Java SE 25 & JDK 25 [build 1])
A constant indicating that the ... for this key). Example: the right shift key. ... A constant indicating that the key event originated on the numeric keypad or with a virtual key corresponding to the numeric keypad. ... Constructs a KeyEvent object....
🌐
Oracle
docs.oracle.com › javase › 8 › javafx › api › javafx › scene › input › KeyEvent.html
KeyEvent (JavaFX 8)
4 weeks ago - Contains multiple elements if the ... or if the key produced multiple Unicode characters itself. For example, character will have the value "A" for a key typed event generated by pressing SHIFT + 'a'. For key pressed and key released events, character is always CHAR_UND...
Top answer
1 of 4
5

e is the KeyEvent.

if you want to see the e value, then you can try this

System.out.println(e.getKeyChar());

Creating KeyEvent :

KeyEvent e = new KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation);

Example (dunno if this is the right way, but it produce the right output):

Button a = new Button("click");
    KeyEvent e;
    e = new KeyEvent(a, 1, 20, 1, 10, 'a');
    System.out.println(""+e.getKeyChar());
    System.out.println(""+e.getKeyCode());

Here is the all type of KeyEvent parameters

java.​awt.​event.​KeyEvent
@Deprecated public KeyEvent(Component source, int id, long when, int modifiers, int keyCode)
Deprecated. as of JDK1.1

===

java.​awt.​event.​KeyEvent
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar)
Constructs a KeyEvent object.
Note that passing in an invalid id results in unspecified behavior. This method throws an IllegalArgumentException if source is null.
Parameters:
source - the Component that originated the event id - an integer identifying the type of event when - a long integer that specifies the time the event occurred modifiers - the modifier keys down during event (shift, ctrl, alt, meta) Either extended _DOWN_MASK or old _MASK modifiers should be used, but both models should not be mixed in one event. Use of the extended modifiers is preferred. keyCode - the integer code for an actual key, or VK_UNDEFINED (for a key-typed event) keyChar - the Unicode character generated by this event, or CHAR_UNDEFINED (for key-pressed and key-released events which do not map to a valid Unicode character) 
Throws:
IllegalArgumentException - if id is KEY_TYPED and keyChar is CHAR_UNDEFINED; or if id is KEY_TYPED and keyCode is not VK_UNDEFINED IllegalArgumentException - if source is null

===

java.​awt.​event.​KeyEvent
public KeyEvent(Component source, int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation)
2 of 4
2

When using robot, first obtain focus of component to which you add your KeyListener to. Then you can use robot to simulte key presses. As an alternative, you can just use dispatchEvent on component to which your listener is added.

KeyEvent key = new KeyEvent(inputField, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, 'Z');
inputField.dispatchEvent(key);

Providing you have:

JInputField InputField = new JInputField();

You can as well create KeyEvent as described above and pass it to keyTyped method of your listener. As for keyPrssed, you can do the same.

Top answer
1 of 4
12

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html Check this tutorial

If it's a UI based application , then " I also need to know what I need to add to my code so that my program waits about 700 milliseconds for a keyinput before moving on to another method" you can use GlassPane or Timer class to fulfill the requirement.

For key Event:

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
        dx = -1;
    }

    if (key == KeyEvent.VK_RIGHT) {
        dx = 1;
    }

    if (key == KeyEvent.VK_UP) {
        dy = -1;
    }

    if (key == KeyEvent.VK_DOWN) {
        dy = 1;
    }
}

check this game example http://zetcode.com/tutorials/javagamestutorial/movingsprites/

2 of 4
11

Here is an SSCCE,

package experiment;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class KeyListenerTester extends JFrame implements KeyListener {

    JLabel label;

    public KeyListenerTester(String s) {
        super(s);
        JPanel p = new JPanel();
        label = new JLabel("Key Listener!");
        p.add(label);
        add(p);
        addKeyListener(this);
        setSize(200, 100);
        setVisible(true);

    }

    @Override
    public void keyTyped(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            System.out.println("Right key typed");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key typed");
        }

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            System.out.println("Right key pressed");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key pressed");
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            System.out.println("Right key Released");
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            System.out.println("Left key Released");
        }
    }

    public static void main(String[] args) {
        new KeyListenerTester("Key Listener Tester");
    }
}

Additionally read upon these links : How to Write a Key Listener and How to Use Key Bindings

🌐
GeeksforGeeks
geeksforgeeks.org › java › java-keylistener-in-awt
Java KeyListener in AWT - GeeksforGeeks
6 days ago - import java.awt.*; import java.awt.event.*; public class KeyListenerExample extends Frame implements KeyListener { private TextField textField; private Label displayLabel; // Constructor public KeyListenerExample() { // Set frame properties setTitle("Typed Text Display"); setSize(400, 200); setLayout(new FlowLayout()); // Create and add a TextField textField = new TextField(20); textField.addKeyListener(this); add(textField); // Create and add a Label displayLabel = new Label("Typed Text: "); add(displayLabel); // Handle window closing addWindowListener(new WindowAdapter() { public void window
🌐
E Computer Notes
ecomputernotes.com › home › java › swing › keyevent java example
KeyEvent Java Example - Computer Notes
March 28, 2014 - The associated event, java.awt.event.KeyEvent has the following methods of particular interest are listed below: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JavaExampleKeyEvent extends JFrame implements KeyListener { private JLabel LblPrmpt = new JLabel("Press keys as Desired Below:"); private JLabel LblOutPt = new JLabel("Key Typed is:"); private JTextField Txt = new JTextField(10); public JavaExampleKeyEvent() { setTitle("Java Example of Key Event"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); add(LblPrmpt,BorderLayout.NOR
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › desktop java › awt › event
KeyListener Java Example - Examples Java Code Geeks - 2026
April 14, 2021 - " + e.isActionKey()); } private String keyboardLocation(int keybrd) { switch (keybrd) { case KeyEvent.KEY_LOCATION_RIGHT: return "Right"; case KeyEvent.KEY_LOCATION_LEFT: return "Left"; case KeyEvent.KEY_LOCATION_NUMPAD: return "NumPad"; case KeyEvent.KEY_LOCATION_STANDARD: return "Standard"; case KeyEvent.KEY_LOCATION_UNKNOWN: default: return "Unknown"; } } }; JTextField textField = new JTextField(); textField.addKeyListener(listener); contentPane.add(textField, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); } } In this section, we will see an example implementation where the KeyL