Sunday, May 18, 2014

Control Mouse Move in Java


Simple Note

Use java.awt.Robot class.

Code

package proxy.remoteproxy;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Robot;

public class TestMain {
    public static void main (String args[]) throws AWTException, InterruptedException {
        // get X/Y position of mouse cursor
        int posX = (int)MouseInfo.getPointerInfo().getLocation().getX();
        int posY = (int)MouseInfo.getPointerInfo().getLocation().getY();

        Robot bot = new Robot();
        // move around a rect
        bot.mouseMove(posX-30, posY-30);
        Thread.sleep(1000);
        bot.mouseMove(posX+30, posY-30);
        Thread.sleep(1000);
        bot.mouseMove(posX+30, posY+30);
        Thread.sleep(1000);
        bot.mouseMove(posX-30, posY+30);
        Thread.sleep(1000);
        bot.mouseMove(posX-30, posY-30);
        Thread.sleep(1000);
        // back to start position
        bot.mouseMove(posX, posY);
    }
}


Reference

Class Robot

No comments:

Post a Comment