How to simulate keyboard presses in java?
java.awt.Robot might help.
Here's a simple sample code snippet from Java Tips:
try {
Robot robot = new Robot();
// Simulate a mouse click
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
// Simulate a key press
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
} catch (AWTException e) {
e.printStackTrace();
}
How to simulate Keyboard press in java
You can use Robot
for this, as shown in this example. To get {
, for example, you'll need to do something like this:
keyPress(KeyEvent.VK_SHIFT);
keyPress(KeyEvent.VK_OPEN_BRACKET);
keyRelease(KeyEvent.VK_OPEN_BRACKET);
keyRelease(KeyEvent.VK_SHIFT);
Simulate keyboard press
You can try KeyListener
in java.
http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html
Java simulate keyboard INT input (Robot)
Simply use
robot.keyPress(KeyEvent.VK_0);
robot.keyRelease(KeyEvent.VK_0);
There is no way to "truly" simulate that an integer comes from the keyboard. The keyboard delivers ascii characters, and it doesn't know which once that qualify as integers.
Related Topics
Is the Order Guaranteed for the Return of Keys and Values from a Linkedhashmap Object
How to Sort an Arraylist of Objects by a Property
How to Keep a Scanner from Throwing Exceptions When the Wrong Type Is Entered
How to Simulate a Buffered Peripheral Device with Swingworker
Spring Jdbc Template for Calling Stored Procedures
How to Get SQL from Hibernate Criteria API (*Not* for Logging)
Testing Private Method Using Mockito
Differencebetween Identity and Equality in Oop
Java: "Final" System.Out, System.In and System.Err
How to Add a Maven Dependency in Eclipse
What's Up with Java's "%N" in Printf
Safe String to Bigdecimal Conversion
What Is the Cross-Platform Way of Obtaining the Path to the Local Application Data Directory