Java Examples of Robot applications with robot functions

  • 2021-01-14 05:54:41
  • OfStack

Most of the time, we want automated testing, automated demonstration, or some other mouse or keyboard control application (such as helping people click on ads to earn money). For this purpose, since JDK1.3, it has provided us with a robotic human for generating native input events - java.awt.Robot.

Let me introduce Robot functions and application examples in detail:

1. Main functions of Robot

1. BufferedImage createScreenCapture(Rectangle screenRect)

Description: This method provides a function similar to the PrintScreen key on a keyboard. It generates 1 BufferedImage from the screen pixel copy in the specified rectangular area.
Application: we can use this method in the graphics program, or use it to achieve remote screen transmission, can be made into a remote computer monitoring program.

2. void delay(int ms)

Description: Used to put the current program (thread) to sleep (sleep) for several milliseconds (ms).
Application: can be used to control program delay. This is usually necessary because you will have a delay between two separate operations.

3. Color getPixelColor(int x, int y)

Description: Gets the color value at the pixel position of the given screen coordinate.
Application: is to take the color RGB value, not much to say.

4. void keyPress(int keycode)
void keyRelease(int keycode)

Note: the role of these two methods 1 see, used to produce the specified key press down and lift action, equivalent to Win32 API keyb_event function, that is, to simulate the keyboard operation, the specific keycode value is KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_CONTROL what, the specific application directly see Eclipse prompt.
Application: It can be used for automatic demonstration, testing and so on. It is very useful.

5. void mouseMove(int x, int y)

Description: Moves the mouse cursor to the specified screen coordinate.
Application: can be used for the program of automatic demonstration, testing, with other methods of use, is indispensable.

6. void mousePress(int buttons)
void mouseRelease(int buttons)
void mouseWheel(int wheelAmt)

Note: The above 3 methods generate the specified mouse button press, lift, and scroll action, is to simulate the operation of the mouse, the specific buttons values are InputEvent.BUTTON1_MASK (left mouse button), InputEvent.BUTTON3_MASK (right mouse, if it is a double key mouse, please use InputEvent.BUTTON2_MASK), etc.

Application: 1 kind can also be used in the program of automatic demonstration, testing, with other methods used, very important.

2. Application examples

I wrote two relatively small application examples, one is a simple simulation test, the other is automatically click on the advertisement to earn profits, the following are demonstrated.

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, to achieve the basic functions  
 * @author Alexia 
 * @date 2013/5/18 
 * 
 */ 
public class Common { 
 
  /** 
   *  Mouse click (left click) , To double click, call continuously  
   * 
   * @param r 
   * @param x 
   *      x coordinates  
   * @param y 
   *      y coordinates  
   * @param delay 
   *       The delay time after the operation  
   */ 
  public static void clickLMouse(Robot r, int x, int y, int delay) { 
    r.mouseMove(x, y); 
    r.mousePress(InputEvent.BUTTON1_MASK); 
    r.delay(10); 
    r.mouseRelease(InputEvent.BUTTON1_MASK); 
    r.delay(delay); 
 
  } 
 
  /** 
   *  Right click the mouse , To double click, call continuously  
   * 
   * @param r 
   * @param x 
   *      x coordinates  
   * @param y 
   *      y coordinates  
   * @param delay 
   *       The delay time after the 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 ( 1 Secondary input only 1 Char)  
   * 
   * @param r 
   * @param ks 
   *       An array of characters entered by the keyboard  
   * @param delay 
   *       The input 1 The delay time after keys  
   */ 
  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(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(300, 300); 
    r.mousePress(InputEvent.BUTTON1_MASK); 
    r.mouseRelease(InputEvent.BUTTON1_MASK); 
    r.keyPress(KeyEvent.VK_CONTROL); 
    r.keyPress(KeyEvent.VK_V); 
    r.keyRelease(KeyEvent.VK_CONTROL); 
    r.keyRelease(KeyEvent.VK_V); 
  } 
 
  /** 
   *  Capture full screen mousse  
   * 
   * @param r 
   * @return 
   */ 
  public Icon captureFullScreen(Robot r) { 
    BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle( 
        Toolkit.getDefaultToolkit().getScreenSize())); 
    ImageIcon icon = new ImageIcon(fullScreenImage); 
    return icon; 
  } 
 
  /** 
   *  screen-capturing 1 Orthopaedic areas  
   * 
   * @param r 
   * @param x 
   *      x coordinates  
   * @param y 
   *      y coordinates  
   * @param width 
   *       Rectangular wide  
   * @param height 
   *       Rectangular high  
   * @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 icon = new ImageIcon(fullScreenImage); 
    return icon; 
  } 
 
} 

Before the example, notice how to determine the screen coordinate position, I downloaded a small tool, it is 10 minutes convenient to use, I suggest you to use

1. Simple simulation tests


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 the enter  
        rb.keyPress(KeyEvent.VK_ENTER); 
        rb.keyRelease(KeyEvent.VK_ENTER); 
      } 
    }.start(); 
 
    rb.delay(3000); 
 
    //  Set the approximate location of the Start Menu  
    int x = 40; 
    int y = Toolkit.getDefaultToolkit().getScreenSize().height - 10; //  Mouse over to the Start menu , 
    rb.mouseMove(x, y); 
    rb.delay(500); 
 
    //  Click the Start Menu  
    Common.clickLMouse(rb, x, y, 500); 
     
    rb.delay(1000); 
 
    //  run CMD The 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 DIR The 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 CLS The 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 EXIT The 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 the test  
    x = Toolkit.getDefaultToolkit().getScreenSize().width - 50; 
    Common.clickRMouse(rb, x, y, 500); 
 
    new Thread() { 
      public void run() { 
        rb.delay(1000); //  enter  
        rb.keyPress(KeyEvent.VK_ENTER); 
        rb.keyRelease(KeyEvent.VK_ENTER); 
      } 
    }.start(); 
 
    JOptionPane.showMessageDialog(null, " The presentation !"); 
  } 
} 

2. Click on netease ads 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 (AWTException ex) { 
 
      ex.printStackTrace(); 
 
    } 
  } 
 
  public void init() { 
 
    robot.delay(3000); 
     
    System.out.println("Click Ads start"); 
 
    //  Opens the specified browser in a new browser window or in an existing browser window URL(JDK 1.6 The above ) 
    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 Auto-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 down arrow to turn 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 down arrow to turn the page  
      for (int i = 0; i < 500; i++) 
        Common.pressKeys(robot, ks, 0); 
 
      //  Enter the down arrow to turn 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.clickLMouse(robot, x, y, 3000); 
 
      System.out.println(" Successfully click No. " + count + " An advertisement! "); 
    } 
 
  } 
 
  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(); 
 
  } 
} 

Related articles: