获取鼠标位置

问题描述:

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

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

我怎么能知道没有做任何事情(什么不可见)的位置?

谢谢

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

+40

'getPointerInfo()。getLocation()'返回相对于屏幕的位置。如果你想要相对于你的组件的位置(就像MouseListeners给出的那样),你可以从它减去'yourComponent.getLocationOnScreen()'。 – 2012-01-10 10:43:03

+2

+1'如果鼠标移动速度太快,'Container.getMousePosition()'有时会返回'null',这样可以避免这个问题。 – 2014-02-07 21:21:06

+0

我们是否可以知道鼠标在自然屏幕上的点击位置 – Srb 2014-05-30 08:55:24

如果您使用的Swing为你的UI层,你可以使用这个Mouse-Motion Listener

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

如果您正在使用SWT,您可能需要考虑添加一个MouseMoveListener,如here所述。

+4

但只有我用鼠标执行某些操作(移动,单击)才会执行监听器,对吗?我在搬运前需要的第一件事就是到起始位置 – 2009-09-17 14:05:19

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); 
+13

请加下一次知道一些意见。 – CSchulz 2012-02-06 22:44:16

在SWT中,您无需在侦听器中查看鼠标位置。 Display对象具有方法getCursorLocation()

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

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

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

在我的场景中,我应该根据用鼠标完成的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; 
} 

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

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 
     } 

    } 

} 

我做这样的事情让鼠标坐标使用机器人,我在一些比赛,我发展的进一步使用这些坐标:

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; 
      } 
     } 
    } 
}