Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Java program to contol computer mouse using the awt Robot and events

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default 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:

    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:

    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:

    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:

    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);
     
     }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers


  2. #2
    Junior Member
    Join Date
    Jan 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Control your computers mouse using the Robot Class

    Very nice function dude, appreciate your work.

  3. #3
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    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?

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default 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.

  7. #7

    Default 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?

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default 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.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Control your computers mouse using the Robot Class

    Quote Originally Posted by nambill View Post
    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") ;-)

Similar Threads

  1. How to get Mouse Position even if it is not within our application?
    By Freaky Chris in forum Java Programming Tutorials
    Replies: 2
    Last Post: January 4th, 2012, 09:57 AM
  2. How to get your computer name and IP address?
    By JavaPF in forum Java Networking Tutorials
    Replies: 7
    Last Post: December 8th, 2011, 11:36 AM
  3. How to Sendkeys to an application in Java using the Robot Class
    By JavaPF in forum Java SE API Tutorials
    Replies: 6
    Last Post: August 4th, 2011, 12:13 AM
  4. Java program to open and close computer CD/DVD drive
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 28th, 2009, 11:04 AM
  5. How can i control keyboard through programming?
    By Mohd in forum Java Theory & Questions
    Replies: 3
    Last Post: January 5th, 2009, 06:10 AM