首页 文章

获取鼠标位置

提问于
浏览
92

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

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

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

谢谢

10 回答

  • 2

    如果您正在使用SWT,则可能需要查看添加MouseMoveListener,如here所述 .

  • 3

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

  • 186
    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);
    
  • 0

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

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

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

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

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

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

  • 35

    如果您使用Swing作为UI图层,则可以使用Mouse-Motion Listener .

  • 4

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

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

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

相关问题