簡體   English   中英

Java鼠標在屏幕上的任何位置移動

[英]Java mouse motion anywhere on screen

我確信這是可能的,但我所有的搜索都是空白的。

在Java中,是否可以在Java應用程序之外注冊鼠標運動事件? 因此,如果鼠標指針移動到屏幕上的任何位置,我會收到回電。 輪詢MouseInfo.getPointerInfo可以進行近似,但必須有更好的方法。

謝謝

解釋用例:它僅用於寵物項目,但基本上在鼠標觸及屏幕邊緣時觸發事件。 我還在想,如果你試圖超越屏幕的邊緣,可能會觸發不同的事件。 為此,我認為鼠標運動監聽器可能更合適。

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接收mouseDragged事件。 那是因為無法接收有關主窗口外點擊的信息。
  • 出於類似的原因,每個MouseEventmodifiers將始終為0。
  • 對於值clickCountpopupTriggerbutton
  • 您將需要提供一個虛擬的java.awt.Component ,它將用作MouseEvent的(假)源 - 此處不允許使用null值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM