[FPS Camera: Robot.mouseMove() persistency] *closed*

Hey JGO.

CLOSED:
Wouldn’t work with my current setup if implemented with persistency at the moment anyways, thanks for the feedback everyone.

What I’m attempting to do:
I’m attempting to implement a robot to my window that will ‘always’ keep the mouse centered in the middle of it.
While maybe letting the mouse move 1 pixel each way (maximum), just so we can detect the direction. (not really important as of now)

Problems:
If the mouse is moved rather fast (direction doesn’t matter), the robot doesn’t ‘keep up’ and persistently center it. :emo:
If the mouse is moved fast enough, it is possible to get the mouse out of the window all together. :expressionless:

Questions:
How can I implement this in a persistent way? (While debugging the mouse is visible, I’d like to see it constantly centered)
Is this just a common ordeal when dealing with a Window not in full screen?
How is this commonly done?

Code at the moment:


	public void mouseMoved(MouseEvent m) {
		mouseX = m.getX() - 8; // Compensation for JFrame edge width (keeps minimum mouseX == 0)
		mouseY = m.getY() - 30; // Compensation for JFrame title height (keeps minimum mouseY == 0)
		 
		final int windowLocationX = getLocationOnScreen().x;
		final int windowLocationY = getLocationOnScreen().y;
		final int centerX = windowLocationX + getWidth() / 2;
		final int centerY = windowLocationY + getHeight() / 2;

		if (mouseX != centerX || mouseY != centerY) {
			if (mouseX != centerX) { /* ... */ }
			if (mouseY != centerY) { /* ... */ }
			robot.mouseMove(centerX, centerY);
		}
	}

Notes:
If someone thinks the reason I’m not getting the persistency I’d prefer is due to the reason of my implementation of compensating for the window’s borders, I’ve executed the program without compensation and it made no difference.
I have tried executing this every frame instead of only executing it when the mouse is moved, still no difference.

Any feedback’s welcome guys, thanks.

I see what you’re trying to do, but note that hard-coding the size of the window’s title bar and borders is a bad idea. Different OSes will have different values, let alone if the user has different themes, font sizes, display settings, etc. You will need to handle such information dynamically.

As for the mouse events, you’ll never get one for each pixel the mouse moves. Your best bet might be to also listen for mouseExited() events and move the mouse back on them as well. You’ll most likely need to handle mouseDragged() events the same way too, since they won’t get handled by your mouseMoved() handler.

Hey thanks, never knew that.
Regarding mouseDragged, mouseMoved is called inside mouseDragged so centering will still be handled and dragging discarded.

EDIT
: Regarding mouseExited() and moving the mouse back in, that still doesn’t constrain the mouse location to the window.