Java鼠标在屏幕上的任何位置移动 [英] Java mouse motion anywhere on screen

查看:317
本文介绍了Java鼠标在屏幕上的任何位置移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm sure this is possible but all my searching is coming up blank.

在Java中,可以在Java之外注册鼠标运动事件应用程序吗?因此,如果鼠标指针移动到屏幕上的任何位置,我会收到回电。轮询 MouseInfo.getPointerInfo 可以进行近似,但必须有更好的方法。

In Java is it possible to register for a mouse motion event outside of a Java app? So if the mouse pointer moves anywhere on the screen I get a call back. An approximation is possible with polling MouseInfo.getPointerInfo but there must be a better way.

谢谢

解释用例:
它只适用于宠物项目,但基本上在鼠标触及屏幕边缘时触发事件。我还在想,如果你试图推过屏幕的边缘,可能会触发不同的事件。为此,我认为鼠标运动监听器可能更合适。

To explain the use case: It's just for a pet project but basically firing events when the mouse hits the edge of the screen. I was also thinking that different events could be fired if you try to push past the edge of the screen. And for this I thought a mouse motion listener might be more appropriate.

推荐答案

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

java.awt.event.MouseMotionListener is only going to give you information about mouse movement inside your application window. For events that occur outside that window, there is no way around MouseInfo.getPointerInfo. However, you could write a (potentially singleton) class that polls the pointer info in regular intervals and allows MouseMotionListeners to be added:

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存在一些显着差异虽然:

Beware that there are some notable differences from your standard MouseMotionListener though:


  • 您只会收到 mouseMoved 事件,永远不会 mouseDragged 事件。这是因为无法接收有关主窗口外点击的信息。

  • 出于类似的原因,每个<$的修饰符 c $ c> MouseEvent 将始终为0.

  • clickCount 也是如此, popupTrigger 按钮

  • 您需要提供虚拟 java.awt.Component 将用作 MouseEvent 的(假)来源 - null此处不允许使用值。

  • You will only receive mouseMoved events, never mouseDragged events. That's because there is no way to receive information about clicks outside the main window.
  • For similar reasons, the modifiers of each MouseEvent will be always be 0.
  • The same goes for the values clickCount, popupTrigger, button
  • You will need to provide a dummy java.awt.Component that will be used as the (fake) source of the MouseEvents - null values are not allowed here.

这篇关于Java鼠标在屏幕上的任何位置移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆