Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Program to demonstrate how to create Mouse Event using the Robot class in Java

Program to demonstrate how to create Mouse Event using the Robot class in Java

package com.hubberspot.robot;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class RobotMouseEvent {

 
   public static void main(String[] args) {
  
 try {

            Robot robot = new Robot();
   
     robot.delay(2000);
   
     //moves the mouse to (x,y) position
     robot.mouseMove(100, 100);
   
     robot.delay(2000);
     robot.mouseMove(500, 500);
     //moves the mouse to (x,y) and left clicks mouse button
     robot.mousePress(InputEvent.BUTTON1_MASK);
     robot.mouseRelease(InputEvent.BUTTON1_MASK);
      
     robot.delay(2000);
     robot.mouseMove(300, 300);
     //moves the mouse to (x,y) and right clicks mouse button
     robot.mousePress(InputEvent.BUTTON3_MASK);
     robot.mouseRelease(InputEvent.BUTTON3_MASK);
       
     robot.delay(2000);
     robot.mouseMove(600, 600);
     robot.delay(2000);
     //moves the mouse to (x,y) and middle wheel clicks 
     robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
     robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
     // it scrolls the mouse wheel
     robot.mouseWheel(-100);
  
  
 } catch (AWTException e) {
   
    e.printStackTrace();
    }
  
    }

}



 
© 2021 Learn Java by Examples Template by Hubberspot