Robot function of Java robot application example
Many times, we hope to realize automatic testing, automatic demonstration function, 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 machine human - java.awt.robot
Let me introduce the functions and application examples of robot in detail:
1、 Main functions of robot
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 rectangular area to generate a bufferedimage. Application: we can use this method in graphics program, or use it to realize remote screen transmission, and make remote computer monitoring program, etc
2. void delay(int ms)
Description: used to sleep the current program (thread) for several milliseconds (MS). Application: it can be used to control the delay of the program. This is generally necessary because you must have a delay in two interval operations.
3. Color getPixelColor(int x,int y)
Description: get the color value of the pixel position of the given screen coordinate. Application: just take the color RGB value, so I won't say more.
4. void keyPress(int keycode) void keyRelease(int keycode)
Note: the functions of these two methods can be seen at a glance. They are used to generate the key pressing and lifting actions of the specified key, which is equivalent to the keyb of Win32 API_ Event function, which simulates keyboard operation. The specific keycode value is keyevent VK_ C、KeyEvent. VK_ D、KeyEvent. VK_ Control or something. Just look at the eclipse prompt for specific applications. Application: it can be used for automatic demonstration and testing 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 and testing of programs. It is indispensable to cooperate 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 wheel action of the specified mouse button, which is to simulate the mouse operation. The specific values of buttons are inputevent BUTTON1_ Mask (left mouse button), inputevent.buton3_mask (right mouse button, if it is a double button mouse, please use inputevent.buton2_mask), etc.
Application: it can also be used for automatic demonstration and testing of programs. It is very important to cooperate 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 advertising to make profits. The following are demonstrated respectively.
First write some common methods java
Before the example, pay attention to how to determine the screen coordinate position. I downloaded a small tool, which is very convenient to use. I suggest you use it
1. Simple simulation test
2. Click Netease ads to earn meager profits
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.