Java Implementation Automation robot class simple application

Source: Internet
Author: User
Tags save file

/**
* 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);
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.