代码之家  ›  专栏  ›  技术社区  ›  David

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

  •  8
  • David  · 技术社区  · 16 年前

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

    在Java中,是否可以在Java应用程序之外注册鼠标运动事件?因此,如果鼠标指针在屏幕上的任何地方移动,我会得到一个回电。通过轮询可以得到近似值 MouseInfo.getPointerInfo 但一定有更好的办法。

    谢谢

    要解释用例: 这只是一个宠物项目,但基本上发射事件时,鼠标点击屏幕的边缘。我也在想,如果你试着 推开 屏幕的边缘。对于这个,我认为鼠标运动监听器可能更合适。

    1 回复  |  直到 16 年前
        1
  •  12
  •   Thomas    11 年前

    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 事件。这是因为无法接收有关主窗口外单击的信息。
    • 出于类似的原因 modifiers MouseEvent 将始终为0。
    • 价值观也是如此 clickCount , popupTrigger , button
    • 你需要提供一个假人 java.awt.Component 这将被用作(伪造的)犯罪来源 鼠标事件 s码- null 此处不允许值。
    推荐文章