java - 在 Java 中捕获(捕获)窗口中的鼠标光标

标签 java mouse capture

我正在寻找一种方法,在鼠标进入窗口后捕获或捕获鼠标,就像鼠标被捕获在虚拟机窗口中,直到用户按下 CTRL+ALT+DEL 或在某些情况下释放鼠标其他方式。我如何在 Java 中实现这一点?全屏显示不是一种选择。

编辑:

这是给你的一些 SSCCE。此代码会将您的鼠标困在窗口中。要退出,您只需在生成的框架内并直接移动到关闭按钮。如果您注意到鼠标试图离开时它会自动返回到 (0,0)。我需要知道的是如何让它回到它退出的坐标。我尝试用 getX() 和 getY() 代替 (0,0),但机器人没有将鼠标返回到那里(我认为响应时间很慢)。我还让机器人将鼠标移回 crosshair.x 和 crosshair.y,但是如果用户在正确的时刻单击,这(以及其他)仍然允许鼠标逃脱。有什么想法吗?

主类:

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferStrategy;
import java.awt.image.MemoryImageSource;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class Game extends JFrame implements MouseMotionListener, MouseListener{

    private int windowWidth = 640;
    private int windowHeight = 480;
        private Crosshair crosshair;

    public static void main(String[] args) {
        new Game();
    }

    public Game() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(windowWidth, windowHeight);
        this.setResizable(false);
        this.setLocation(0,0);
        this.setVisible(true);

        this.createBufferStrategy(2);
                addMouseMotionListener(this);
                addMouseListener(this);
        initGame();

        while(true) {
            long start = System.currentTimeMillis();
            gameLoop();
            while(System.currentTimeMillis()-start < 5) {
                            //empty while loop
            }
        }
    }

    private void initGame() {
            hideCursor();
            crosshair = new Crosshair (windowWidth/2, windowHeight/2);
    }

        private void gameLoop() {
            //game logic
            drawFrame();
        }

        private void drawFrame() {

            BufferStrategy bf = this.getBufferStrategy();
            Graphics g = (Graphics)bf.getDrawGraphics();
            try {
                g = bf.getDrawGraphics();
                Color darkBlue = new Color(0x010040);
                g.setColor(darkBlue);
                g.fillRect(0, 0, windowWidth, windowHeight);
                drawCrossHair(g);
            } finally {
                g.dispose();
            }
            bf.show();
            Toolkit.getDefaultToolkit().sync();
        }

        private void drawCrossHair(Graphics g){
            Color yellow = new Color (0xEDFF62);
            g.setColor(yellow);
            g.drawOval(crosshair.x, crosshair.y, 40, 40);

            g.fillArc(crosshair.x + 10, crosshair.y + 21 , 20, 20, -45, -90);
            g.fillArc(crosshair.x - 1, crosshair.y + 10, 20, 20, -135, -90);
            g.fillArc(crosshair.x + 10, crosshair.y - 1, 20, 20, -225, -90);
            g.fillArc(crosshair.x + 21, crosshair.y + 10, 20, 20, -315, -90);
        }

        @Override
        public void mouseDragged(MouseEvent e) {
        //empty method
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        crosshair.x = e.getX();
        crosshair.y = e.getY();
        }

        private void hideCursor() {
            int[] pixels = new int[16 * 16];
            Image image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16, 16, pixels, 0, 16));
            Cursor transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "invisiblecursor");
            getContentPane().setCursor(transparentCursor);
    }

        public void mouseExited(MouseEvent e) {
            System.out.println("Event: " + e);
            try {
                Robot robot = new Robot();
                robot.mouseMove(0, 0);// When I use (getX(),getY()) instead of (0,0) the robot will not move the mouse at all even though getX() and getY() are the coordinates I want the mouse to be moved to.  Also the mouse can still escape, even when crosshair.x and crosshair.y are used as the coordinates.  It seems that robot is too slow.
            }
            catch (AWTException ex) {
                ex.printStackTrace();
            }
        }

        public void mouseEntered(MouseEvent e){
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseClicked(MouseEvent e) {
        }
}

另一个类:

public class Crosshair{
        public int x;
    public int y;
    public Crosshair(int x, int y) {
        this.x = x;
        this.y = y;
        }
}

最佳答案

我想你可以使用 Global Event Listener监听框架的鼠标输入事件。然后在鼠标退出事件上,我想您可以使用机器人在鼠标离开框架后重置鼠标的位置。

关于java - 在 Java 中捕获(捕获)窗口中的鼠标光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2481804/

相关文章:

java - 以特定树编码获取从根到叶的路径

Java反射读取内部类值

android - Webrtc Android 摄像头捕获

c++ - 如何定义一个lambda函数来捕获类的 'this'指针?

java - 是否可以用 JSR-330 @Scope 变体替换 Spring @Scope ("request")?

java - Java Attach API 使用什么进程间通信机制?

events - 我从哪里开始制作 linux 输入法?

java - 在java中不断获取鼠标相对于Jframe的位置

java - 鼠标监听器不起作用

javascript - 如何截取网页的屏幕截图?