获取鼠标位置

时间:2009-09-17 13:53:09

标签: java mouse

我想模拟Java中的自然鼠标移动(从这里到像素逐行)。要做到这一点,我需要知道起始坐标。

我找到了方法event.getX()和event.getY(),但我需要一个事件......

如何在不做任何事情(或不可见的事情)的情况下知道这些职位?

谢谢

10 个答案:

答案 0 :(得分:189)

MouseInfo.getPointerInfo().getLocation()可能会有所帮助。它返回与当前鼠标位置对应的Point对象。

答案 1 :(得分:35)

PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
System.out.print(y + "jjjjjjjjj");
System.out.print(x);
Robot r = new Robot();
r.mouseMove(x, y - 50);

答案 2 :(得分:10)

在SWT中,您无需在听众中找到鼠标位置。 Display对象的方法为getCursorLocation()

在vanilla SWT / JFace中,请致电Display.getCurrent().getCursorLocation()

在RCP应用程序中,请致电PlatformUI.getWorkbench().getDisplay().getCursorLocation()

对于SWT应用程序,最好使用getCursorLocation()而不是其他人提到的MouseInfo.getPointerInfo(),因为后者是在SWT旨在替换的AWT工具包中实现的。

答案 3 :(得分:7)

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 + ")");
    }
  }
}

答案 4 :(得分:6)

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 
        }

    }

}

答案 5 :(得分:4)

尝试查看java.awt.Robot类。它允许您以编程方式移动鼠标。

答案 6 :(得分:2)

如果您使用Swing作为UI图层,则可以使用Mouse-Motion Listener进行此操作。

答案 7 :(得分:1)

我正在做这样的事情来使用Robot来获取鼠标坐标,我在我正在开发的几个游戏中进一步使用这些坐标:

public class ForMouseOnly {
    public static void main(String[] args) throws InterruptedException {
        int x = MouseInfo.getPointerInfo().getLocation().x;
        int y = MouseInfo.getPointerInfo().getLocation().y;
        while (true) {

            if (x != MouseInfo.getPointerInfo().getLocation().x || y != MouseInfo.getPointerInfo().getLocation().y) {
                System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + ", "
                        + MouseInfo.getPointerInfo().getLocation().y + ")");
                x = MouseInfo.getPointerInfo().getLocation().x;
                y = MouseInfo.getPointerInfo().getLocation().y;
            }
        }
    }
}

答案 8 :(得分:0)

如果您正在使用SWT,则可能需要按照here的说明添加MouseMoveListener。

答案 9 :(得分:0)

在我的场景中,我应该根据用鼠标完成的GUI操作在鼠标位置打开一个对话框。以下代码对我有用:

    public Object open() {
    //create the contents of the dialog
    createContents();
    //setting the shell location based on the curent position
    //of the mouse
    PointerInfo a = MouseInfo.getPointerInfo();
    Point pt = a.getLocation();
    shellEO.setLocation (pt.x, pt.y);

    //once the contents are created and location is set-
    //open the dialog
    shellEO.open();
    shellEO.layout();
    Display display = getParent().getDisplay();
    while (!shellEO.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    return result;
}