Java 获取鼠标位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1439022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:16:34  来源:igfitidea点击:

Get Mouse Position

javamouse

提问by Martin Trigaux

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.

我想用 Java 模拟一个自然的鼠标移动(从这里到那里一个像素一个像素)。为此,我需要知道起始坐标。

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

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

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

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

Thank you

谢谢

采纳答案by vpram86

MouseInfo.getPointerInfo().getLocation()might be helpful. It returns a Pointobject corresponding to current mouse position.

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

回答by Andrzej Doyle

If you're using Swing as your UI layer, you can use a Mouse-Motion Listenerfor this.

如果您使用 Swing 作为 UI 层,则可以为此使用鼠标运动侦听器

回答by Jeff Storey

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

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

回答by Topher Fangio

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

如果您使用SWT,你可能想看看添加一个MouseMoveListener作为解释在这里

回答by Nauman Khalid

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

回答by stevorino

In SWT you need not be in a listener to get at the mouse location. The Display object has the method getCursorLocation().

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

In vanilla SWT/JFace, call Display.getCurrent().getCursorLocation().

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

In an RCP application, call PlatformUI.getWorkbench().getDisplay().getCursorLocation().

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

For SWT applications, it is preferable to use getCursorLocation()over the MouseInfo.getPointerInfo()that others have mentioned, as the latter is implemented in the AWT toolkit that SWT was designed to replace.

对于SWT应用中,优选使用getCursorLocation()MouseInfo.getPointerInfo(),其他人已经提到的那样,因为后者在AWT工具包,SWT被设计用来替换实现。

回答by sudipn

In my scenario, I was supposed to open a dialog box in the mouse position based on a GUI operation done with the mouse. The following code worked for me:

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

回答by Srb

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

回答by Josh McQuinn

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 
        }

    }

}

回答by Varun Ved

I am doing something like this to get mouse coordinates using Robot, I use these coordinates further in few of the games I am developing:

我正在做这样的事情来使用 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;
            }
        }
    }
}