Java鼠标移动屏幕上的任何位置

10

我相信这是可能的,但我搜索了很多都找不到答案。

在Java中,是否可以注册一个超出Java应用程序范围的鼠标运动事件?所以,如果鼠标指针在屏幕上任何位置移动,我就会得到一个回调。使用轮询MouseInfo.getPointerInfo可以实现近似,但必须有更好的方法。

谢谢

为了解释用例: 这只是一个宠物项目,但基本上是当鼠标碰到屏幕边缘时触发事件。我还在考虑,如果你试图越过屏幕的边缘,则可能会触发不同的事件。为此,我认为鼠标运动监听器可能更合适。


我认为没有更好的方法了。这必须是独立于操作系统的,我怀疑这很容易实现。但是,如果您定期轮询MouseInfo.getPointerInfo方法,有什么问题吗?你的用例是什么? - Karussell
1个回答

13

java.awt.event.MouseMotionListener 只能提供关于鼠标在应用程序窗口内移动的信息。对于发生在该窗口之外的事件, 没有绕过 MouseInfo.getPointerInfo 的方法。但是,你可以编写一个(可能是单例)类,在固定时间间隔内轮询指针信息,并允许添加 MouseMotionListeners

import java.awt.Component;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

/**
 * This class checks the position every #DELAY milliseconds and 
 * informs all registered MouseMotionListeners about position updates.
 */
public class MouseObserver {
    /* the resolution of the mouse motion */
    private static final int DELAY = 10;

    private Component component;
    private Timer timer;
    private Set<MouseMotionListener> mouseMotionListeners;

    protected MouseObserver(Component component) {
        if (component == null) {
            throw new IllegalArgumentException("Null component not allowed.");
        }

        this.component = component;

        /* poll mouse coordinates at the given rate */
        timer = new Timer(DELAY, new ActionListener() {
                private Point lastPoint = MouseInfo.getPointerInfo().getLocation();

                /* called every DELAY milliseconds to fetch the
                 * current mouse coordinates */
                public synchronized void actionPerformed(ActionEvent e) {
                    Point point = MouseInfo.getPointerInfo().getLocation();

                    if (!point.equals(lastPoint)) {
                        fireMouseMotionEvent(point);
                    }

                    lastPoint = point;
                }
            });
        mouseMotionListeners = new HashSet<MouseMotionListener>();
    }

    public Component getComponent() {
        return component;
    }

    public void start() {
        timer.start();
    }

    public void stop() {
        timer.stop();
    }

    public void addMouseMotionListener(MouseMotionListener listener) {
        synchronized (mouseMotionListeners) {
            mouseMotionListeners.add(listener);
        }
    }

    public void removeMouseMotionListener(MouseMotionListener listener) {
        synchronized (mouseMotionListeners) {
            mouseMotionListeners.remove(listener);
        }
    }

    protected void fireMouseMotionEvent(Point point) {
        synchronized (mouseMotionListeners) {
            for (final MouseMotionListener listener : mouseMotionListeners) {
                final MouseEvent event =
                    new MouseEvent(component, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(),
                                   0, point.x, point.y, 0, false);

                SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            listener.mouseMoved(event);
                        }
                    });
            }
        }
    }

    /* Testing the ovserver */
    public static void main(String[] args) {
        JFrame main = new JFrame("dummy...");
        main.setSize(100,100);
        main.setVisible(true);

        MouseObserver mo = new MouseObserver(main);
        mo.addMouseMotionListener(new MouseMotionListener() {
                public void mouseMoved(MouseEvent e) {
                    System.out.println("mouse moved: " + e.getPoint());
                }

                public void mouseDragged(MouseEvent e) {
                    System.out.println("mouse dragged: " + e.getPoint());
                }
            });

        mo.start();
    }
}

请注意,与您标准的MouseMotionListener相比,有一些值得注意的差异:

  • 您仅会收到mouseMoved事件,而永远不会收到mouseDragged事件。这是因为没有办法接收关于主窗口外点击的信息。
  • 出于类似的原因,每个MouseEventmodifiers值始终为0。
  • clickCountpopupTriggerbutton的值也是如此。
  • 您需要提供一个虚拟的java.awt.Component作为MouseEvent的源(假源)——这里不允许使用空值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接