Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials


Reply
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 20-01-2009, 09:19 AM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
Default How to Control your computers mouse using the Robot Class

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);
 
 }
}
Click the left mouse button:

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);
 
 }
}
Click the right mouse button:

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);
 
 }
}
Click & scroll the mouse wheel:

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.
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 26-01-2009, 03:50 PM
Junior Member
 

Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Nirinium is on a distinguished road
Default Re: How to Control your computers mouse using the Robot Class

Very nice function dude, appreciate your work.
__________________
Reply With Quote
  #3 (permalink)  
Old 11-02-2009, 08:08 PM
Fendaril's Avatar
Member
 

Join Date: Nov 2008
Posts: 54
Thanks: 7
Thanked 5 Times in 5 Posts
Fendaril is on a distinguished road
Default 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?
Reply With Quote
  #4 (permalink)  
Old 11-02-2009, 08:17 PM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
Default Re: How to Control your computers mouse using the Robot Class

Quote:
Originally Posted by Fendaril View Post
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
__________________
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:05 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.