How do I create key press event using Robot class?

Category: java.awt, viewed: 10512 time(s).

In this example we use the java.awt.Robot class to generate a key press event. We can call the keyPress(int keyCode) method to produce this event.

package org.kodejava.example.awt;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class CreatingKeyboardEvent {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            //
            // Create a three seconds delay.
            //
            robot.delay(3000);
                                     
            //
            // Genering key press event for writing the QWERTY letters
            //
            robot.keyPress(KeyEvent.VK_Q);
            robot.keyPress(KeyEvent.VK_W);
            robot.keyPress(KeyEvent.VK_E);
            robot.keyPress(KeyEvent.VK_R);
            robot.keyPress(KeyEvent.VK_T);
            robot.keyPress(KeyEvent.VK_Y);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats