In-depth Java robot implementation of the control of the mouse and keyboard methods detailed _java

Source: Internet
Author: User
the Java.awt.Robot class is used to control the mouse and keyboard. Once you get this kind of control, you can do any type of operation with the mouse and keyboard through your Java code. This class is typically used for automated testing. The following code sample shows you how the robot class handles keyboard events. If you run this code and open Notepad, you will see Hi caoer in Notepad. Try it quickly.
Copy Code code as follows:

Import java.awt.AWTException;
Import Java.awt.Robot;
Import java.awt.event.KeyEvent;
public class Robotexp {
public static void Main (string[] args) {
try {
Robot Robot = new Robot ();
Define a 5-second delay so you can open Notepad.
Robot began to write
Robot.delay (5000);
Robot.keypress (Keyevent.vk_h);
Robot.keypress (keyevent.vk_i);
Robot.keypress (Keyevent.vk_space);
Robot.keypress (Keyevent.vk_c);
Robot.keypress (KEYEVENT.VK_A);
Robot.keypress (Keyevent.vk_o);
Robot.keypress (keyevent.vk_e);
Robot.keypress (Keyevent.vk_r);
catch (Awtexception e) {
E.printstacktrace ();
}
}
}

users have perfected the above code:
Copy Code code as follows:

Import java.awt.AWTException;
Import Java.awt.Robot;
Import java.awt.event.KeyEvent;
Import java.io.IOException;
public class Robotexp {
public static void Presskey (Robot Robot, int keyvalue) {
Robot.keypress (keyvalue);
Robot.keyrelease (keyvalue);
}
public static void Presskeywithshift (Robot Robot, int keyvalue) {
Robot.keypress (Keyevent.vk_shift);
Robot.keypress (keyvalue);
Robot.keyrelease (keyvalue);
Robot.keyrelease (Keyevent.vk_shift);
}
public static void Closeapplication (Robot Robot) {
Presskey (robot, Keyevent.vk_alt);
Presskey (robot, KEYEVENT.VK_F4);
Robot.keypress (Keyevent.vk_alt);
Robot.keypress (KEYEVENT.VK_F4);
Robot.keyrelease (Keyevent.vk_alt);
Robot.keyrelease (KEYEVENT.VK_F4);
For Linux.
Robot.keypress (Keyevent.vk_alt);
Robot.keypress (KEYEVENT.VK_W);
Robot.keyrelease (Keyevent.vk_alt);
Robot.keyrelease (KEYEVENT.VK_W);
Robot.keypress (Keyevent.vk_n);
Robot.keyrelease (Keyevent.vk_n);
}
public static void Main (string[] args) throws IOException {
try {
Robot Robot = new Robot ();
Runtime.getruntime (). EXEC ("notepad");
For Linux.
Runtime.getruntime (). EXEC ("gedit");
Define a 5-second delay so you can open notepad haha
Robot began to write
Robot.delay (3000);
for (int i = 0; i < i++) {
Presskeywithshift (robot, Keyevent.vk_h);
Presskey (robot, keyevent.vk_i);
Presskey (robot, Keyevent.vk_space);
Presskeywithshift (robot, Keyevent.vk_h);
Presskeywithshift (robot, keyevent.vk_i);
Presskey (robot, Keyevent.vk_space);
Presskey (robot, KEYEVENT.VK_A);
Presskey (robot, keyevent.vk_m);
Presskey (robot, Keyevent.vk_space);
Presskey (robot, keyevent.vk_t);
Presskey (robot, Keyevent.vk_h);
Presskey (robot, keyevent.vk_e);
Presskey (robot, Keyevent.vk_space);
Presskey (robot, Keyevent.vk_j);
Presskey (robot, KEYEVENT.VK_A);
Presskey (robot, Keyevent.vk_v);
Presskey (robot, KEYEVENT.VK_A);
Presskey (robot, Keyevent.vk_space);
Presskey (robot, Keyevent.vk_r);
Presskey (robot, Keyevent.vk_o);
Presskey (robot, Keyevent.vk_b);
Presskey (robot, Keyevent.vk_o);
Presskey (robot, keyevent.vk_t);
Vk_enter
Presskey (robot, Keyevent.vk_enter);
Presskey (Robot, KeyEvent.);
}
Closeapplication (robot);
Robot.keypress (Keyevent.vk_space);
catch (Awtexception e) {
E.printstacktrace ();
}
}
}

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.