Home > database >  How to move cursor and click on a specific pixel in java screen
How to move cursor and click on a specific pixel in java screen

Time:05-06

I am trying to move cursor to a specific pixel in the screen and click on it using a java program.

I tried to capture a screenshot then work on it but I am stuck.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle)

CodePudding user response:

You can click on a specific Pixel with the mouse using Robot class in java and move it using move method.

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

public class MouseClicker {

    static Robot rb;

    public static void main(String[] args) {
        try {
            rb = new Robot();
        } catch (AWTException e) {
            System.out.println("Error while creating object"   e);
        }
        rb.mouseMove(1360, 768); //this method takes two parameters (Height, Width)
        rb.mousePress(InputEvent.BUTTON1_DOWN_MASK);//this method takes one parameter (BUTTON) to press it
        rb.delay(10); //this is the delay between every press
        rb.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);//this method takes one parameter (BUTTON) to release it
    }
}

For more reference you can see the Oracle Docs.

  • Related