Java.awt.Robot class controls the mouse and keyboard

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.

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();
//定义5秒的延迟以便你打开notepad
// Robot 开始写
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:

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.