Simulate mouse and keyboard input in Java __java

Source: Internet
Author: User
Starting from jdk1.3, a robot class is provided, under the java.awt. * Package. This class is described in the JDK as follows:

This class is used to automate the test, run the demo, and other applications that require control of the mouse and keyboard to input events from the computer system. The main purpose of Robot is to facilitate automated testing on the Java platform.

The difference between using this class to generate an input event and sending an event to an AWT event queue or an AWT component is that the event is generated in the platform's native input queue. For example, Robot.mousemove will actually move the mouse cursor instead of just creating mouse movement events.

Note that some platforms require specific permissions or extensions to access low-level input controls. If the input control is not allowed for the current platform configuration, an attempt to construct the Robot object throws a awtexception. For example, if the X server does not support (or is not enabled) Xtest 2.2 standard extensions, the X-window system throws an exception.

Applications that use Robot for purposes other than testing should properly handle these error conditions.


In addition to simulating mouse keyboard operation, this class can also be used to intercept the screen, only to demonstrate how to simulate mouse keyboard operation, the specific API reference Javadoc. This demo completes the pop-up QQ and mobile window function. The code is as follows:

Import java.awt.AWTException;
Import Java.awt.Robot;
Import java.awt.event.KeyEvent;
/** */ /**
* @author Bean
*
*/
public class Robotdemo ... {

Private Robot Robot = null;

Public Robotdemo () ... {
Try ... {
Robot = new Robot ();
catch (Awtexception e) ... {
E.printstacktrace ();
}
}
/** *//** can eject QQ * *
public void Keyboarddemo () ... {
Robot.keypress (Keyevent.vk_alt);
Robot.keypress (Keyevent.vk_control);
Robot.keypress (keyevent.vk_z);
Robot.keyrelease (keyevent.vk_z);
Robot.keyrelease (Keyevent.vk_control);
Robot.keyrelease (Keyevent.vk_alt);
}
/** *//** premise is to have a maximized window, the function is to move to the title bar, and then drag to 600,600 of the position * *
public void Mousedemo () ... {
Robot.mousemove (80, 10);
Robot.mousepress (Keyevent.button1_mask);
Try ... {
Thread.Sleep (20);
catch (Interruptedexception e) ... {
E.printstacktrace ();
}
Robot.mousemove (600, 600);
Robot.mouserelease (Keyevent.button1_mask);
}
/** *//**
* @param args
*/
public static void Main (string[] args) ... {
Robotdemo demo=new Robotdemo ();
Demo.keyboarddemo ();
Demo.mousedemo ();
}

}

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.