Capture (capture) the mouse cursor in a Java window

I'm looking for a way to capture or capture the mouse after it enters the window, just as the mouse is trapped in the virtual machine window, until the user presses Ctrl Alt del or releases the mouse in other ways How do I achieve this in Java? Full screen is not an option

Edit:

Here are some of your sscce This code captures the mouse into the window To leave, you just need to move directly to the close button within the generated frame If you notice the mouse trying to leave, it will automatically return (0,0) What I need to know is how to get it back to its exit coordinates I try to use geTx () and gety () instead of (0,0), but the robot won't return the mouse there (I think the response time will slow down) I also asked the robot to move the mouse back to crosshair X and crosshair Y but if the user clicks at the right time, this (and others) still allows the mouse to escape Any ideas?

Main category:

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,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,-45,-90);
            g.fillArc(crosshair.x - 1,crosshair.y + 10,-135,-90);
            g.fillArc(crosshair.x + 10,crosshair.y - 1,-225,-90);
            g.fillArc(crosshair.x + 21,-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,16));
            Cursor transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(image,new Point(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) {
        }
}

Another category:

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

Solution

I think you can use the global event listener to listen for mouse input events of frames Then on the mouse exit event, I think you can use the robot to reset the position when the mouse leaves the frame

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>