/**
* Robot class:
*
* EXEC function declaration:
* Public Process EXEC (String command) throws IOException, parameters and function Description:
* Command: A specified system command
* Function: Executes the specified string command in a separate process
*
* KeyPress Function Description:
* public void keyPress (int keycode), Parameters and function Description:
* KeyCode: The key to press (for example, KEYEVENT.VK_A)
* Function: Analog press the specified key
*
* Keyrelease Function Description:
* public void Keyrelease (int keycode), Parameters and function Description:
* KeyCode: The key to release
* Function: Simulate release specified key
*
* @param TOVEP
*/
Import Java.awt.Robot;
Import java.awt.event.KeyEvent;
public class Exce {
public static void Main (string[] args) {
try{
Create an automatic action class
Robot Robot = new Robot ();
/**
*
* The way to run a Word program using the runtime class is:
* Runtime.getruntime (). EXEC ("cmd/c start Winword");
* The system commands in parentheses
*
*/
Start Notepad Program
Runtime.getruntime (). EXEC ("cmd/c start Notepad");
Delay for a few seconds and wait for the Notepad program to start successfully
Robot.delay (3000);
Simulate pressing the "Ctrl + space" key combination to start the input method
Presskeywithctrl (Robot,keyevent.vk_space);
Simulate randomly press 100 letters and enter Chinese characters
for (int i=0;i<100;i++) {
Presskey (robot, (int) (Math.random () *) + ' A ');
Presskey (Robot,keyevent.vk_space);
}
Delay for 5 seconds, and observe altogether.
Robot.delay (5000);
Close Notepad
Closeapplication (robot);
}catch (Exception e) {
System.out.println (E.getmessage ());
}
}
Simulate pressing the keyboard character keys
public static void Presskey (Robot Robot, int keyvalue) {
Simulate Press
Robot.keypress (keyvalue);
Simulate bouncing up
Robot.keyrelease (keyvalue);
}
Simulate pressing the CTRL key and the character keys at the same time
public static void Presskeywithctrl (Robot Robot, int keyvalue) {
Simulate Press
Robot.keypress (Keyevent.vk_control);
Robot.keypress (keyvalue);
Simulate bouncing up
Robot.keypress (keyvalue);
Robot.keyrelease (Keyevent.vk_control);
}
Simulate pressing the "ALT + F4" key combination to close the current application
public static void Closeapplication (Robot Robot) {
Simulate pressing the "ALT + F4" key combination
Simulate Press
Robot.keypress (Keyevent.vk_alt);
Robot.keypress (KEYEVENT.VK_F4);
Simulate bouncing up
Robot.keyrelease (Keyevent.vk_alt);
Robot.keyrelease (KEYEVENT.VK_F4);
Analog Press "N", do not save file Exit Notepad program
Simulate Press
Robot.keypress (Keyevent.vk_n);
Simulate bouncing up
Robot.keyrelease (Keyevent.vk_n);
}
}