Introduction to the Java Robot Class in Java
Now let's move along to another program that demonstrates use of the keyboard by a Robot object.
Description of the program named Robot05
In addition to creating low-level mouse inputs, an object of the Robot class can create low-level keyboard inputs, just as though a human user were pressing the keys on the keyboard.
Designed to work specifically with Windows OS
As written, this program will run correctly only under the Windows operating system. This is because it specifically makes use of the Windows program named Notepad.
(However, the program could easily be modified to use a similar editor program under a different operating system.)
Low-level keyboard input
This program illustrates the low-level nature of the behavior of an object of the Robot class by:
- Starting the Windows Notepad editor program in a separate process.
- Using a Robot object to enter text into the Notepad document.
Running the program
This Java program does not have a GUI. To see the intended behavior of this program, simply start the program running from the command line under the Windows operating system.
Notepad program will start
When this Java program starts running, it causes the Windows Notepad program to start running in a separate process with a new empty document.
Enter text into the Notepad document and terminate
Then this program uses a Robot object to enter the word Hello into the Notepad document. After that, the Java program terminates.
When the Java program terminates, the Notepad program is still running, and can be saved to a file, or terminated by the user.
Requires SDK V1.3 or later
As mentioned earlier, the Robot class was first released in SDK Version 1.3. Therefore, as was the case in the previous program, you will need to be running Version 1.3 or later to successfully compile and execute this program. This program was tested on my machine using SDK 1.4.1 under WinXP
Will discuss in fragments
As usual, I will discuss this program in fragments. You can view a complete listing of Robot05 in Listing 11 near the end of the lesson.
Need some keycode data
The purpose of this program is to demonstrate the ability of a Java program to enter character data into a non-Java program. Therefore, we will need some keycode data.
The code in
Listing 6 creates an array object containing the virtual keycodes for the
word hello. This data will be used later with the
Robot.keyPress and Robot.keyRelease methods, and the
KeyEvent.VK_SHIFT virtual keycode, to mimic the behavior of a human user entering
the characters Hello into a Notepad document.
public class Robot05{ //Create an array of keycode data int keyInput[] = { KeyEvent.VK_H, KeyEvent.VK_E, KeyEvent.VK_L, KeyEvent.VK_L, KeyEvent.VK_O };//end keyInput array Listing 6 |
Virtual keycodes are used
In one of the technical documents on its site, Sun has this to say about specifying keycodes for use with a Robot object:
"Notice that virtual keycodes are used to enter keystrokes; keycodes are not the same as Java characters. KeyEvent.VK_A corresponds to pressing the unshifted 'A' key on a keyboard. If you specify 'A' or 'a' instead of KeyEvent.VK_A, you get unexpected results."
Start Notepad running
The main method begins in Listing 7. The code in Listing 7 starts the Notepad program running in a separate process. This should cause the Notepad program to start running and to become the active window, capable of accepting input from the keyboard.
public static void main(String[] args) throws AWTException,IOException{ Runtime.getRuntime().exec("notepad"); Listing 7 |
Runtime is not affiliated with the Robot class
Note that the exec method used in Listing 7 has nothing to do with the Robot class. The Runtime class and its methods have been available since JDK1.0.
Here is what Sun has to say about the Runtime class.
"Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method."
Methods of the Runtime class
The Runtime class provides about twenty instance methods that allow you do unrelated things such as:
- Determine the amount of free memory in the Java Virtual Machine.
- Run the garbage collector.
- Forcibly terminate the currently running Java virtual machine.
- Run the finalization methods of any objects pending finalization.
The exec method of the Runtime class
In addition, the exec method of the Runtime class makes it possible to "execute a specified string as a command in a separate process."
The code in Listing 7 executes the string "notepad" as a command in a separate process. This, in turn, causes the Windows program named Notepad to start running and to become the active window.
Get a Robot object
The code in Listing 8 gets and saves a reference to a new Robot
object, which will be used to enter text characters into the Notepad edit
window.
Robot robot = new Robot(); Listing 8 |
Enter the characters into the Notepad edit window
The code in Listing 9 extracts the keycodes previously stored in the array
and invokes the keyPress method to mimic the action of a human
typist. This causes the characters represented by those keycodes to be
entered into the Notepad editor.
robot.keyPress(KeyEvent.VK_SHIFT); for (int cnt2 = 0; cnt2 < keyInput.length; cnt2++){ if(cnt2 > 0){ robot.keyRelease(KeyEvent.VK_SHIFT); }//end if robot.keyPress(keyInput[cnt2]); //Insert a one-half second delay between // characters. robot.delay(500); }//end for loop }//main }//end class Robot05 Listing 9 |
Note upper and lower case characters
Note the combined use of the keyPress and keyRelease methods along with KeyEvent.VK_SHIFT virtual keycode to mimic the user holding down the Shift key for entry of the first character, H.
A one-half second delay
The code in Listing 9 also inserts a one-half second delay following the entry of each character so that you can see them being individually entered into the Notepad editor.
End of the program
The code in Listing 9 also signals the end of the main method and the end of the program.
After all of the characters have been entered into the Notepad editor, the Java program terminates.
Recap for program Robot05
The program named Robot05 starts a new instance of the program named Notepad running in a separate process, and then enters the characters Hello into the Notepad document. Then the Java program terminates, leaving the Notepad program running.
Page 3 of 4
This article was originally published on May 27, 2003