|
||
|
||||
|
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: Java Code
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);
}
}
Java Code
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);
}
}
Java Code
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);
}
}
Java Code
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);
}
}
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight] Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
|
||||
|
Quote:
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
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight] Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
LinkBacks (?)
LinkBack to this Thread: http://www.javaprogrammingforums.com/java-code-snippets-tutorials/214-how-control-your-computers-mouse-using-robot-class.html
|
||||
| Posted By | For | Type | Date | |
| Java Programming Forums - JavaProgrammingForums.com | This thread | Refback | 22-01-2009 03:14 PM | |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to Get your computers name and IP address | JavaPF | Java Code Snippets and Tutorials | 2 | 30-04-2010 01:42 PM |
| How to Sendkeys to an application in Java using the Robot Class | JavaPF | Java Code Snippets and Tutorials | 4 | 25-10-2009 03:40 AM |
| How to get Mouse Position | Freaky Chris | Java Code Snippets and Tutorials | 0 | 29-04-2009 05:41 PM |
| How to Open & close the computers CD/DVD drive | JavaPF | Java Code Snippets and Tutorials | 0 | 28-01-2009 04:04 PM |
| [SOLVED] How I can control Keyboard? | Mohd | Java Theory & Questions | 3 | 05-01-2009 11:10 AM |