Get Mouse Position

2019-01-02 15:25发布

I would like to simulate a natural mouse movement in Java (going from here to there pixel by pixel). To do that I need to know the starting coordinates.

I've found the method event.getX() and event.getY() but I need an event...

How can I know the positions without doing anything (or something not visible)?

Thank you

标签: java mouse
10条回答
回忆,回不去的记忆
2楼-- · 2019-01-02 16:11

Try looking at the java.awt.Robot class. It allows you to move the mouse programatically.

查看更多
笑指拈花
3楼-- · 2019-01-02 16:12

If you're using SWT, you might want to look at adding a MouseMoveListener as explained here.

查看更多
路过你的时光
4楼-- · 2019-01-02 16:14
import java.awt.MouseInfo;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class MyClass {
  public static void main(String[] args) throws InterruptedException{
    while(true){
      //Thread.sleep(100);
      System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + 
              ", " + 
              MouseInfo.getPointerInfo().getLocation().y + ")");
    }
  }
}
查看更多
荒废的爱情
5楼-- · 2019-01-02 16:15
import java.awt.MouseInfo;
import java.util.concurrent.TimeUnit;

public class Cords {

    public static void main(String[] args) throws InterruptedException {

        //get cords of mouse code, outputs to console every 1/2 second
        //make sure to import and include the "throws in the main method"

        while(true == true)
        {
        TimeUnit.SECONDS.sleep(1/2);
        double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
        double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
        System.out.println("X:" + mouseX);
        System.out.println("Y:" + mouseY);
        //make sure to import 
        }

    }

}
查看更多
登录 后发表回答