This tutorial is about java Robot class. (java.awt.Robot)
I will quote Sun here:
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)."This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The
primary purpose of Robot is to facilitate automated testing of Java platform implementations."
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:
Then we will need to make a Robot object:Code:import java.awt.Robot;
Now we have access to our Robot methods. (The complete list of methods and javadoc can be found here)Code:Robot r = new Robot();
MOUSE:
which moves your cursor instantly to the specified location.Code:r.mouseMove(x, y);
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.
Presses and instantly releases your specified button.Code:r.mousePress(buttons); r.mouseRelease(buttons);
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:
(I'm not sure, how they depend on the mouse, platform, configuration, etc, so you should check them out yourself.)Code:InputEvent.BUTTON1_MASK //left button InputEvent.BUTTON2_MASK //middle button InputEvent.BUTTON3_MASK //right button
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.Code:r.mouseWheel(wheelAmt);
KEYBOARD:
Presses the specified key and releases it after.Code:r.keyPress(key); r.keyRelease(key);
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:
If you use proper IDE, you are able to find them all easily.Code:r.keyPress(KeyEvent.VK_A); // press A-key r.keyRelease(KeyEvent.VK_A); // release A-key
OTHER:
Robot class can also make screenshots or get the pixel color on screen:
Creating screenshot requires Rectangle (java.awt.Rectangle) object as an argument, as this is the specified area, from where the screenshot is taken.Code:r.createScreenCapture(new Rectangle(0, 0, 100, 100));
The result is BufferedImage (java.awt.image.BufferedImage) which can then be handeled properly (analyzed, saved or whatever needs to be done).
Gets you the pixel color as Color(java.awt.Color) object.Code:r.getPixelColor(x, y);
Sleeps the specified amount of milliseconds. Is very useful when writing a Robot program. Is pretty much interchanceable with Thread.sleep(ms).Code:r.delay(ms);
USEFUL STUFF TO KNOW WHEN WRITING A ROBOT
Get current screen resolution:
returns a Dimenson(java.awt.Dimension)Code:Toolkit.getDefaultToolkit().getScreenSize();
Get current mouse position
returns a Point(java.awt.Point)Code:MouseInfo.getPointerInfo().getLocation();
---
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:
Have funNote that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects.![]()
Good tutorial! rep+. You could have linked this page though, the full documentation of the java.awt.Robot class which tell all existing methods etc.
http://java.sun.com/j2se/1.3/docs/ap...awt/Robot.html
Thanks.
But i did link to that page, check right above the Mouse chapter.
Oh, sorry! missed that
Good and useful tutorial rep+ Thanks You...
so would you use r.getPixelColor() to try to get Robot to react to things happening on the screen?
Like could you create a boolean function, such as hasSomethingHappened() which keeps checking the pixel color, & just get the program to keep sleeping till it returns true?
Is there any more efficient way to get Robot to interact with screen events?
I wouldn't use r.getPixelColor(). I would use r.createScreenCapture() and get the Colors from there, i'm pretty sure, it is more efficient way for large data access than getPixelColor().
You have to create your own artificial intelligence for the machine, no 'easy way' here. It's very hard to create something that could actually understand what's on the screen. Neural Networks would be one possibility.
This has happened to me several timesAgain, 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!i let the robot control the mouse and click with it. But i got stuck in a loop several times and because it was constantly clicking i couldn't kill the process with the keyboard because task manager would lose focus. So now i always set up a timer which stops the program after a set time (mostly about 10-20 seconds)
![]()
Nice job Sinipull!
@Sinipull, I am trying to build up an automation program to do some specific jobs.
Considering the capabilities of robot class, it works fine but i am stuck at two things:
1. Is there anyway to know if a process is finished in another application before pressing a key for example.Can someone please answer this?
Like, if i run e.g a windows media player, how would i know it is loaded in OS and ready to be used, before
i click further using robot.xxx
2. Using robot, we can read the mouse and write back both mouse/keyboard actions. But is there anyway to
read the keyboard as well?
Like, if i automate documets and want my robot class to wait for some specific input from user, for (say) a
particular field, and rest it has to fill automatically, how can it read the key input?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks