获取鼠标位置

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

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

我怎样才能知道这些职位而不做任何事情(或不可见的事情)?

谢谢

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

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

在SWT中,你不需要在监听器中获取鼠标位置。 Display对象具有getCursorLocation()方法。

在vanilla SWT / JFace中,调用Display.getCurrent().getCursorLocation()

在RCP应用程序中,调用PlatformUI.getWorkbench().getDisplay().getCursorLocation()

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

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

如果您将Swing用作UI层,则可以使用鼠标移动侦听器 。

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

如果您正在使用SWT,则可能需要按照此处所述添加MouseMoveListener。

在我的情况下,我应该打开一个鼠标位置的对话框基于用鼠标完成的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; } 

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

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