Sign in to follow this  
Infinity95

Make cursor stay in window

Recommended Posts

Infinity95    324

Hello guys. I'm having some trouble keeping the cursor in my game window. I have the following code which works quite fine:

if(GetFocus() == Game::window) {
		GetCursorPos(&this->cursorPos);
		ScreenToClient(Game::window, &this->cursorPos);
		POINT pCopy = this->cursorPos;
		if(this->cursorPos.x > windowSize.right) pCopy.x = windowSize.right;
		if(this->cursorPos.x < 0) pCopy.x = 0;
		if(this->cursorPos.y > windowSize.bottom) pCopy.y = windowSize.bottom;
		if(this->cursorPos.y < 0) pCopy.y = 0;
		if(pCopy.x != this->cursorPos.x || pCopy.y != this->cursorPos.y) {
			ClientToScreen(Game::window, &pCopy);
			SetCursorPos(pCopy.x, pCopy.y);
		}
	}

The problem now is that when i go out of the window too quickly with the mouse and click it loses focus. Is there any way to avoid this?

Share this post


Link to post
Share on other sites
Bacterius    13165

I think what a lot of games do is actually disable the mouse cursor and instead use some D3D API to directly read velocity values from the mouse driver. That way the cursor isn't actually moving and you're getting accurate mouse info. Check out Raw Input maybe?

 

If that doesn't work/isn't applicable/overkill, maybe ClipCursor would do the trick? It tends to go away when the user clicks, though, so it wouldn't be foolproof, and you need to remember to disable it when you lose focus. I don't recommend ClipCursor, though. It's probably one of those evil solutions that only corporate managers find elegant.

Edited by Bacterius

Share this post


Link to post
Share on other sites
vipar    171

Usually games just focus the cursor in the middle of the screen or in 0,0 and then have their own cursor that reads the mouse hardware.

 

You might have noticed some times when alt-tabbing out of FPS games too many times in a row.

Share this post


Link to post
Share on other sites
deftware    1778

I make the cursor invisible, set the cursor position to the center of the screen each time I get it's position (each frame), and use the difference between actual position and screen center to determine how to displace a virtual cursor (for GUI purposes) and/or route the detected movement to a camera system. That is of course unless I've detected that the window has lost focus, then I re-enable the cursor's visibility and stop setting it to the center of the screen.

Share this post


Link to post
Share on other sites
Infinity95    324

I make the cursor invisible, set the cursor position to the center of the screen each time I get it's position (each frame), and use the difference between actual position and screen center to determine how to displace a virtual cursor (for GUI purposes) and/or route the detected movement to a camera system. That is of course unless I've detected that the window has lost focus, then I re-enable the cursor's visibility and stop setting it to the center of the screen.

 

Yeah that's how i re-programmed it now. I just make the normal cursor invisible and get the rest of the input with DirectInput now. What's left now is to make my own curser and display it at the according screen position.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this