I will quote Sun here:
Quote
primary purpose of Robot is to facilitate automated testing of Java platform implementations."
So, basically this gives the low-level input control over your mouse and keyboard to your java program. Note, that you are still able to control your mouse and keyboard when the robot is running (in case of correct coding).
IMPORTANT: The Robot class gives your program as much power, as you have over your computer. It can do EVERYTHING you can do with your mouse and keyboard. The program has even ability to write it's own code, while running and these kind of programs should be written very carefully. (Not because Skynet is probable to evolve, but it can wreck something on your computer).
Ok, let's see how to start using the Robot class.
First we need to import the Robot class:
import java.awt.Robot;
Then we will need to make a Robot object:
Robot r = new Robot();
Now we have access to our Robot methods. (The complete list of methods and javadoc can be found here)
MOUSE:
r.mouseMove(x, y);which moves your cursor instantly to the specified location.
Note, that x = 0 is located in the left of your screen and y = 0 is located at the top of your screen.
The largest values, where your cursor can move, is your screen resolution - 1. On my 1680x1050, it is x = 1679 and y = 1049.
If you tell the cursor to move out of the screen area, it will just slam against the respective wall.
r.mousePress(buttons); r.mouseRelease(buttons);Presses and instantly releases your specified button.
IMPORTANT: Never forget to release the button after pressing. (Can mess up something on your computer as a result, if lot of that is done)
the mousePress() and mouseRelease() takes int as an argument, which should be selected from amongst the constants in the InputEvent(java.awt.event.InputEvent) class:
those are the constants:
InputEvent.BUTTON1_MASK //left button InputEvent.BUTTON2_MASK //middle button InputEvent.BUTTON3_MASK //right button
(I'm not sure, how they depend on the mouse, platform, configuration, etc, so you should check them out yourself.)
r.mouseWheel(wheelAmt);This scrolls your mousewheel, if you have one. The wheelAmt argument is an int and should be positive or negative int value, depending how much or which way do you want to scroll the mouse.
KEYBOARD:
r.keyPress(key); r.keyRelease(key);
Presses the specified key and releases it after.
IMPORTANT: Never forget to release the key after pressing. I actually had to restart my computer to fix the damage, that was caused by this mistake. Restarting just Eclipse didn't help.
The "key" argument is again int and should be selected as a constant from KeyEvent(java.awt.event.KeyEvent) class. Now, there are too many constants to copy them all here, so i'll just show you how they look:
r.keyPress(KeyEvent.VK_A); // press A-key r.keyRelease(KeyEvent.VK_A); // release A-key
If you use proper IDE, you are able to find them all easily.
OTHER:
Robot class can also make screenshots or get the pixel color on screen:
r.createScreenCapture(new Rectangle(0, 0, 100, 100));Creating screenshot requires Rectangle (java.awt.Rectangle) object as an argument, as this is the specified area, from where the screenshot is taken.
The result is BufferedImage (java.awt.image.BufferedImage) which can then be handeled properly (analyzed, saved or whatever needs to be done).
r.getPixelColor(x, y);Gets you the pixel color as Color(java.awt.Color) object.
r.delay(ms);Sleeps the specified amount of milliseconds. Is very useful when writing a Robot program. Is pretty much interchanceable with Thread.sleep(ms).
USEFUL STUFF TO KNOW WHEN WRITING A ROBOT
Get current screen resolution:
Toolkit.getDefaultToolkit().getScreenSize();returns a Dimenson(java.awt.Dimension)
Get current mouse position
MouseInfo.getPointerInfo().getLocation();returns a Point(java.awt.Point)
---
Again, beware of writing programs that take over your mouse and keyboard without sleeping. The only possible way to stop it, is the restart of the computer, from the physical button.
It's easy to stop the program from task manager, if only your mouse is stuck somewhere, but if you keyboard is being used too, then you are pretty much forced to plug off your computer.
Therefore, loops are dangerous, if not used properly!

The Robot class may not work on some platforms, as Sun has written:
Quote
Have fun ;)


Sign In
Create Account


Back to top









