Java Robot application example-Robot function, javarobot
In many cases, we want to implement automatic testing, automatic demonstration, or other mouse and keyboard control applications (such as making profits by clicking advertisements ). For this purpose, since JDK1.3, it has provided us with a machine human-java. awt. Robot.
The following describes the functions and application examples of Robot:
I. Main Robot Functions
1. BufferedImage createScreenCapture (Rectangle screenRect)
Note: This method provides a function similar to the PrintScreen key on the keyboard, which copies the screen pixels in the specified rectangle area to generate a BufferedImage.
Application: we can use this method in graphics programs, or use it to achieve remote screen transmission, which can be used as a remote computer monitoring program.
2. void delay (int MS)
(Used to the current program (thread) sleep) Several milliseconds (MS ).
Application: used to control program latency. This is generally necessary, because there is definitely a latency between the two operations.
3. Color getPixelColor (int x, int y)
Description: gets the color value of the specified screen coordinate pixel position.
Application: Color RGB values.
4. void keyPress (int keycode)
Void keyRelease (int keycode)
Note: The functions of these two methods can be easily understood to generate the pressing and lifting actions of the specified key, which is equivalent to the keyb_event function of Win32 API, that is, simulating keyboard operations, the specific keycode value is KeyEvent. VK_C, KeyEvent. VK_D, KeyEvent. VK_CONTROL or something. You can check the Eclipse prompt for the specific application.
Application: it can be used for automatic demonstration and testing of programs and is very useful.
5. void mouseMove (int x, int y)
Note: move the cursor to the specified screen coordinate.
Application: it can be used for automatic demonstration and testing of programs. It is indispensable to use it with other methods.
6. void mousePress (int buttons)
Void mouseRelease (int buttons)
Void mouseWheel (int wheelAmt)
Note: The above three methods generate the pressing, lifting, and scroll action of the specified mouse button, which is to simulate the mouse operation. The specific buttons value is InputEvent. buttondeskmask (left mouse button), InputEvent. BUTTON3_MASK (right-click the mouse. If it is a double-Key mouse, use InputEvent instead. BUTTON2_MASK.
Application: it can also be used for automatic demonstration and testing of programs. It is very important to use it with other methods.
II. Application Instances
I have written two small application instances, one being a simple simulation test, and the other being an auto-click advertisement to make profits. The following are examples.
First, write some Common methods: Common. java
Package com. alexia; import java. awt. rectangle; import java. awt. robot; import java. awt. toolkit; import java. awt. event. inputEvent; import java. awt. event. keyEvent; import java. awt. image. bufferedImage; import javax. swing. icon; import javax. swing. imageIcon;/*** @ description Robot help class, implement basic functions * @ author Alexia * @ date 2013/5/18 **/public class Common {/*** click (left click ), to double-click it, call ** @ param r * @ param x * x coordinate position * @ param y * y coordinate position * @ param delay * The delay time after this operation */public static void clickLMouse (Robot r, int x, int y, int delay) {r. mouseMove (x, y); r. mousePress (InputEvent. button#mask); r. delay (10); r. mouseRelease (InputEvent. button#mask); r. delay (delay);}/*** right-click the mouse, to double-click it, call ** @ param r * @ param x * x coordinate position * @ param y * y coordinate position * @ param delay * The delay time after this operation */public static void clickRMouse (Robot r, int x, int y, int delay) {r. mouseMove (x, y); r. mousePress (InputEvent. BUTTON3_MASK); r. delay (10); r. mouseRelease (InputEvent. BUTTON3_MASK); r. delay (delay);}/*** keyboard input (only one character can be entered at a time) ** @ param r * @ param ks * array of characters entered on the keyboard * @ param delay * delay Time after a key is entered */public static void pressKeys (Robot r, int [] ks, int delay) {for (int I = 0; I <ks. length; I ++) {r. keyPress (ks [I]); r. delay (10); r. keyRelease (ks [I]); r. delay (delay) ;}}/*** copy ** @ param r * @ throws InterruptedException */void doCopy (Robot r) throws InterruptedException {Thread. sleep (3000); r. setAutoDelay (1, 200); r. keyPress (KeyEvent. VK_CONTROL); r. keyPress (KeyEvent. VK_C); r. keyRelease (KeyEvent. VK_CONTROL); r. keyRelease (KeyEvent. VK_C);}/*** paste ** @ param r * @ throws InterruptedException */void doParse (Robot r) throws InterruptedException {r. setautodelay( 500); Thread. sleep (2000); r. mouseMove (1, 300,300); r. mousePress (InputEvent. button#mask); r. mouseRelease (InputEvent. button#mask); r. keyPress (KeyEvent. VK_CONTROL); r. keyPress (KeyEvent. VK_V); r. keyRelease (KeyEvent. VK_CONTROL); r. keyRelease (KeyEvent. VK_V);}/*** capture full screen MOOC ** @ param r * @ return */public Icon captureFullScreen (Robot r) {BufferedImage fullScreenImage = r. createScreenCapture (new Rectangle (Toolkit. getdefatooltoolkit (). getScreenSize (); ImageIcon icon = new ImageIcon (fullScreenImage); return icon ;} /*** capture an orthopedic area of the screen ** @ param r * @ param x * x coordinate position * @ param y * y coordinate position * @ param width * width of the rectangle * @ param height * height of the rectangle * @ return */public Icon capturePartScreen (Robot r, int x, int y, int width, int height) {r. mouseMove (x, y); BufferedImage fullScreenImage = r. createScreenCapture (new Rectangle (width, height); ImageIcon = new ImageIcon (fullScreenImage); return icon ;}}
Before the example, pay attention to how to determine the coordinates of the screen. I downloaded a small tool, which is very convenient to use. We recommend that you use it.
1. Simple simulated testing
Package com. alexia; import java. awt. *; import java. awt. event. *; import javax. swing. JOptionPane; public class SimpleTest {public static void main (String [] args) throws Exception {final Robot rb = new Robot (); new Thread () {public void run () {rb. delay (2000); // simulate carriage return rb. keyPress (KeyEvent. VK_ENTER); rb. keyRelease (KeyEvent. VK_ENTER );}}. start (); rb. delay (3000); // set the approximate position of the Start menu To int x = 40; int y = Olkit. getdefatooltoolkit (). getScreenSize (). height-10; // move the cursor to the Start Menu, rb. mouseMove (x, y); rb. delay (500); // click Start Menu Common. clickLMouse (rb, x, y, 500); rb. delay (1000); // run the CMD command cmd enter int [] ks = {KeyEvent. VK_C, KeyEvent. VK_M, KeyEvent. VK_D, KeyEvent. VK_ENTER,}; Common. pressKeys (rb, ks, 500); rb. mouseMove (400,400); rb. delay (500); // run the DIR command dir enter ks = new int [] {KeyEvent. VK_D, KeyEvent. VK_ I, KeyEvent. VK_R, KeyEvent. VK_ENTER}; Common. pressKeys (rb, ks, 500); rb. delay (1000); // run the CLS command cls enter ks = new int [] {KeyEvent. VK_C, KeyEvent. VK_L, KeyEvent. VK_S, KeyEvent. VK_ENTER}; Common. pressKeys (rb, ks, 500); rb. delay (1000); // run the EXIT command exit enter ks = new int [] {KeyEvent. VK_E, KeyEvent. VK_X, KeyEvent. VK_ I, KeyEvent. VK_T, KeyEvent. VK_ENTER}; Common. pressKeys (rb, ks, 500); rb. delay (1000); // Right-click to test x = Toolkit. getdefatooltoolkit (). getScreenSize (). width-50; Common. clickRMouse (rb, x, y, 500); new Thread () {public void run () {rb. delay (1000); // press ENTER rb. keyPress (KeyEvent. VK_ENTER); rb. keyRelease (KeyEvent. VK_ENTER );}}. start (); JOptionPane. showMessageDialog (null, "demo completed! ");}}
2. Click Netease advertisement to earn a small profit
Package com. alexia; import java. awt. AWTException; import java. awt. desktop; import java. awt. robot; import java. awt. event. keyEvent; import java. io. IOException; import java.net. URI; import java. util. random; public class AutoClickAds {private Robot robot; private volatile boolean stop = false;/** Creates a new instance of Main */public AutoClickAds () {try {robot = new Robot ();} catch (AWTExceptio N ex) {ex. printStackTrace () ;}} public void init () {robot. delay( 3000); System. out. println ("Click Ads start"); // open the specified URL in the new browser window or in the existing browser window (JDK 1.6 or later) Desktop desktop = Desktop. getDesktop (); if (Desktop. isDesktopSupported () & desktop. isSupported (Desktop. action. BROWSE) {URI uri = URI. create ("http://lanxuezaipiao.blog.163.com/"); try {desktop. browse (uri);} catch (IOException e) {// TODO Aut O-generated catch block e. printStackTrace () ;}try {run ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();} stop (); System. out. println ("Click Ads stoped");} public void run () throws InterruptedException {int count = 1; while (! Stop) {robot. delay (8000); int x = 576; int y = 567; Random r = new Random (); Common. clickLMouse (robot, x, y, 3000); // enter the downward arrow to flip the page int [] ks = {KeyEvent. VK_DOWN}; for (int I = 0; I <10; I ++) Common. pressKeys (robot, ks, 0); int [] [] a ={{ 500,103}, {500,163}, {500,223}, {500,283}, {500,343 }, {500,403}, {500,463}, {500,523}, {500,583}, {500,643},}; int B = r. nextInt (5); x = a [B] [0]; y = a [B] [1]; Common. clickLMouse (robot, x, y, 1000); // enter the downward arrow to turn pages for (int I = 0; I <500; I ++) Common. pressKeys (robot, ks, 0); // enter the downward arrow to flip the page int [] kups = {KeyEvent. VK_UP}; for (int I = 0; I <3; I ++) Common. pressKeys (robot, kups, 0); x = 900; y = 210; Common. clickLMouse (robot, x, y, 3000); x = 1090; y = 15; Common. clickLMouse (robot, x, y, 3000); x = 900; y = 135; Common. clickLM Ouse (robot, x, y, 3000); System. out. println ("click" + count +! ") ;}} Public synchronized void stop () {stop = true ;} /***** @ param args the command line arguments ** @ throws InterruptedException */public static void main (String [] args) throws InterruptedException {AutoClickAds mc = new AutoClickAds (); mc. init ();}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.