简体   繁体   English

如何在Java Swing中抓住鼠标?

[英]How to grab a mouse in Java Swing?

I'm trying to prevent mouse cursor movement (keep cursor's position at application center) and still be able to handle mouseMoved event in order to rotate a camera in space. 我试图阻止鼠标光标移动(保持光标在应用程序中心的位置),并且仍然能够处理mouseMoved事件,以便在空间中旋转相机。 I tried to do this with java.awt.Robot.mouseMove(int x, int y) but it calls mouseMoved event that I'm using to rotate camera, so camera returns to previous position. 我尝试使用java.awt.Robot.mouseMove(int x, int y)进行此操作,但是它调用了我用于旋转摄像机的mouseMoved事件,因此摄像机返回了先前的位置。

And if you just ignore mouseMoved-Events called by Robot? 如果您只是忽略机器人调用的mouseMoved-Events?

You could save the position, the Robot moved the mouse. 您可以保存位置,机器人会移动鼠标。 If you get a Mouse-Event with exactly these mouse-coordinates, just ignore this event. 如果您获得的鼠标事件恰好具有这些鼠标坐标,则只需忽略此事件。 For me something like this worked: 对我来说,这样的工作:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

public class Test {
    // position, where mouse should stay
    private static final int fixX = 500;
    private static final int fixY = 500;

    private static Robot robo;
    private static JFrame frame;

    public static void main(String[] args) {
        // create robot
        try {
            robo = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }

        // create default frame with mouse listener
        frame = new JFrame("test frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent arg0) {
                move(arg0);
            }

            @Override
            public void mouseMoved(MouseEvent arg0) {
                move(arg0);
            }
        });
        frame.setSize(1000, 1000);
        frame.setVisible(true);
    }

    private static void move(MouseEvent arg0) {
        // check, if action was thrown by robot
        if (arg0.getX() == fixX && arg0.getY() == fixY) {
            // ignore mouse action
            return;
        }
        // move mouse to center (important: position relative to frame!)
        robo.mouseMove(fixX + frame.getX(), fixY + frame.getY());

        // compute and print move position
        int moveX = arg0.getX() - fixX;
        int moveY = arg0.getY() - fixY;
        System.out.println("moved: " + moveX + " " + moveY);
    }
}

The mouse stays at 500/500, you get your mouse movement, but you sometimes see the mouse jumping, because Robot is not fast enough. 鼠标停留在500/500位置,您可以移动鼠标,但是有时您会看到鼠标跳动,因为机器人的速度不够快。

Maybe you could just hide the System-Cursor ( How to hide cursor in a Swing application? ) and draw your own cursor. 也许您可以只隐藏系统光标( 如何在Swing应用程序中隐藏光标? )并绘制自己的光标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM