Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a robot to type characters in Java

I know how to have Robot simulate a Y keypress like so:

    Robot.keyPress(KeyEvent.VK_Y);

But how do I get Robot to press a quote and period?:

".  

Can anyone provide me some reference page or sample code?

like image 764
dfdsfsdfsddsgsrew Avatar asked Jul 10 '11 15:07

dfdsfsdfsddsgsrew


People also ask

What is Java AWT Robot?

The Robot class in the Java AWT package is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

How does Java Robot work?

The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue.


2 Answers

You can't always just use the KeyEvent.VK... variable.

For example on my keyboard the "%" character is above the "5". To use a Robot to type a "5", the code would be:

robot.keyPress(KeyEvent.VK_5); 
robot.keyRelease(KeyEvent.VK_5);

and use a Robot to type a "%", the code would be:

robot.keyPress(KeyEvent.VK_SHIFT); 
robot.keyPress(KeyEvent.VK_5); 
robot.keyRelease(KeyEvent.VK_5);
robot.keyRelease(KeyEvent.VK_SHIFT);
like image 69
camickr Avatar answered Sep 27 '22 22:09

camickr


If you wanted to use Robot, KeyEvent has VK_QUOTE and VK_PERIOD constants. All of these constants and more are available through the KeyEvent API

like image 21
Hovercraft Full Of Eels Avatar answered Sep 27 '22 23:09

Hovercraft Full Of Eels