Simulate ENTER Key On Any Component Using Java

Simulating input events in any component in Java can be done by using the Robot class. But what if I do not want to use the Robot class? Well, it is possible. The code below simulates an ENTER keypress event. The KeyEvent object created can easily be changed to cover any key event that you want. If you noticed, one of the parameters has a value of -1. This means that neither ALT, SHIFT or CONTROL is included.

Related Posts Plugin for WordPress, Blogger...

1 comment

  1. hello hello just thanks for your information about java, by misfortune. the code does not work for now … not what would be the error .. step down here a fragment of code. thanks ..

    has you somethig be…thank

    private void CodigoBarraActionPerformed(java.awt.event.ActionEvent evt) {



    public static void simulateEnterKey(Component c) {
    try {
    KeyEvent ke = new KeyEvent(c, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), -1, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER);
    dispatchEvent(ke, c);
    } catch (Exception ex) { }

    private static void dispatchEvent(final KeyEvent ke, final Component comp) throws Exception {
    if (!SwingUtilities.isEventDispatchThread()) {
    SwingUtilities.invokeAndWait(new Runnable () {
    public void run() {
    } else {

Leave a Reply

Your email address will not be published. Required fields are marked *