Java program to contol computer mouse using the awt Robot and events
With this simple tutorial I will show you how to use the Robot Class in Java to control your computers mouse.
We can change the position of the cursor, left and right click, move the mouse wheel and click the mouse wheel.
For these examples you will need to make sure you import the java.awt.Robot & java.awt.event.InputEvent classes.
Move the mouse cursor position on screen:
Code Java:
import java.awt.Robot;
public class MouseClass {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
// SET THE MOUSE X Y POSITION
robot.mouseMove(300, 550);
}
}
Click the left mouse button:
Code Java:
import java.awt.Robot;
import java.awt.event.InputEvent;
public class MouseClass {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
// LEFT CLICK
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
Click the right mouse button:
Code Java:
import java.awt.Robot;
import java.awt.event.InputEvent;
public class MouseClass {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
// RIGHT CLICK
robot.mousePress(InputEvent.BUTTON3_MASK);
robot.mouseRelease(InputEvent.BUTTON3_MASK);
}
}
Click & scroll the mouse wheel:
Code Java:
import java.awt.Robot;
import java.awt.event.InputEvent;
public class MouseClass {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
// MIDDLE WHEEL CLICK
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
// SCROLL THE MOUSE WHEEL
robot.mouseWheel(-100);
}
}
Re: How to Control your computers mouse using the Robot Class
Very nice function dude, appreciate your work.
Re: How to Control your computers mouse using the Robot Class
I notice that when I do not include "throws exception" after the main method the program raises an error when I try to create a robot object.Why is that?
Re: How to Control your computers mouse using the Robot Class
Quote:
Originally Posted by
Fendaril
I notice that when I do not include "throws exception" after the main method the program raises an error when I try to create a robot object.Why is that?
An exception indicates an abnormal condition that must be properly handled to prevent program termination.
Classes such as the robot class require you to handle these exceptions. The proper way to do this would be to place the code in a try/catch block but as you can see ive used the lazy mans way and have just thrown the exception. Throwing the exception means that its not caught, its just vanishes into thin air ;)
Re: How to Control your computers mouse using the Robot Class
Hey JavaPF, well done:)
But how would i make it move (as we humanly do) move the mouse. So like if i used the moving the mouse stuff on a game that has a basic anti-bot detection that i will look like a human because my mouse if moving humanly.
Re: How to Control your computers mouse using the Robot Class
Well ask yourself how does a mouse move when a human moves it. Then compare that to how the Robot class moves it. Within a simple loop with some human-seeming randomness (semi-circular path vs straight to the point, and some overshooting/undershooting the point to be clicked, and variable speeds of movement) you should be well under way.
Not that I support cheating. You cheater. Rule breaking cheater. for shame
Ummm just a heads up. If this question is for what I think it is for, this unnamed place also scans your hard disk and ram, and looks for things like the Robot class.
Re: How to Control your computers mouse using the Robot Class
Thanks for the nice tip! Can we create some useful/interesting apps from that stuff?
Re: How to Control your computers mouse using the Robot Class
Sure, depending on what you call useful/interesting.
For (as time is short) one example: Plants vs Zombies game. I wrote a program that would listen for key combos and move the mouse. There is a game mode where you start with (iirc) 3000 sun(basically money points) and would allow you to build an entire defense before round 1. Then you just had to last as long as you can. So with the click of one button on the 'robot', it would select the cards from the deck to use, and once in the play screen in about 0.75 second it would lay out the full line of defense. For the length of the first round it would let the defense defend against the zombies and collect the loot. When time ran out for the first round, the robot would exit the game, create a new game from the menu, select the cards, and lay them out again, over and over. I also made another bot mode to cycle through all of the aquariums and feed/water all of the plants as necessary. As this was a tedious and boring task. I had other plans for that toy but the game did not hold my interest long enough to finish it lol... However the point is it was entertaining while it lasted. So to me it was interesting to see that it can work, fun to do, and since it took (or at least sped up) the boring parts, it left more fun time in the game. Since then I have reused that old code to make other things, but that is a story for another time.
Best advice I can give you is just play with it.
Re: How to Control your computers mouse using the Robot Class
Quote:
Originally Posted by
nambill
Thanks for the nice tip! Can we create some useful/interesting apps from that stuff?
Firstly, I'd like to thank JavaPF for this tip (I never imagine there is such a thing to search for). Secondly, my 1st "useful" (maybe to me only) app is to keep my screen always awake (i.e. my status on a communication software, like yahoo messenger, never turns to "I'm away from my desk") ;-)