Many times, we hope to implement automatic testing, automatic demonstration functions, or other mouse and keyboard control applications (such as helping people click on advertisements to make profits, etc.). For this purpose, since JDK1.3, it has provided us with a robot that generates native input events - java.awt.Robot.
Below I will introduce Robot's functions and application examples in detail:
1. Robot's main functions
1. BufferedImage createScreenCapture(Rectangle screenRect)
Note: This method provides a function similar to the PrintScreen key on the keyboard, copying the screen pixels in the specified rectangular area to generate a BufferedImage.
Application: We can use this method in a graphics program, or use it to realize remote screen transmission, which can be made into a remote computer monitoring program, etc.
2. void delay(int ms)
Description: Used to sleep the current program (thread) for several milliseconds (ms).
Application: Can be used to control program delay. This is generally necessary because you will definitely have a delay in the two interval operations.
3. Color getPixelColor(int x, int y)
Description: Get the color value of the pixel position of the given screen coordinates.
Application: Just take the color RGB value, so I won’t say much.
4. void keyPress(int keycode)
void keyRelease(int keycode)
Note: You can see the functions of these two methods at a glance. The key press and lifting actions used to generate the specified key are equivalent to the keyb_event function of the Win32 API, that is, to simulate keyboard operations. The specific keycode values are KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_CONTROL, etc. When applying the specific application, you will know it by looking at the Eclipse prompt.
Application: It can be used for automatic demonstration, testing, etc. of programs, which is very useful.
5. void mouseMove(int x, int y)
Description: Move the mouse cursor to the specified screen coordinates.
Application: It can be used for automatic demonstration, testing, etc. of the program, and is indispensable for use with other methods.
6. void mousePress(int buttons)
void mouseRelease(int buttons)
void mouseWheel(int wheelAmt)
Note: The above three methods generate the press, lift, and scroll wheel movement of the specified mouse button, which simulates the mouse operation. The specific buttons values include InputEvent.BUTTON1_MASK (left mouse button), InputEvent.BUTTON3_MASK (right mouse button, if it is a double-key mouse, please use InputEvent.BUTTON2_MASK instead).
Application: It can also be used for automatic demonstration, testing, etc. of the program. It is very important to use it in conjunction with other methods.
2. Application examples
I have written two relatively small application examples, one is a simple simulation test, and the other is to automatically click on advertisements to make profits. The following are demonstrations.
First write some common methods Common.java
package com.alexa; 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 implement basic functions* @author Alexia * @date 2013/5/18 * */ public class Common { /** * Mouse click (left click), and if you want to double-click, you will call continuously * * @param r * @param x * x coordinate position * @param y * y coordinate position * @param delay * Delay time after this 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, and if you want to double-click, call continuously * * @param r * @param x * x coordinate position * @param y * y coordinate position * @param delay * 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 by keyboard* @param delay * Delay time after entering a key*/ 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 moo* * @param r * @return */ public Icon captureFullScreen(Robot r) { BufferedImage fullScreenImage = r.createScreenCapture(new Rectangle( Toolkit.getDefaultToolkit().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 icon = new ImageIcon(fullScreenImage); return icon; } }Before the example, please pay attention to how to determine the coordinate position of the screen. I downloaded a gadget, which is very convenient to use. It is recommended that you use it.
1. Simple mock test
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 int x = 40; int y = Toolkit.getDefaultToolkit().getScreenSize().height - 10; // Move the mouse 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 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.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, "Demo is over!"); } } 2. Click on NetEase Advertising to earn a meager 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"); // Open the specified URL in a new browser window or an existing browser window (JDK 1.6 or 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 stopped"); } 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 realize page turn 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 achieve page turn for (int i = 0; i < 500; i++) Common.pressKeys(robot, ks, 0); // Enter the down arrow to achieve page turn 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 clicked" + count + "Advertisements!"); } } 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 to everyone's learning and I hope everyone will support Wulin.com more.