Make cursor stay in window

Started by
4 comments, last by Infinity95 10 years, 8 months ago

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?

"Errare humanum est, sed in errare perseverare diabolicum."

Advertisement

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Actually ClipCursor is the correct way to do that.

However, as Bacterius said, you have to make sure you undo ClipCursor if your app loses focus or gets closed.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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.

CEO of Dynamic Realities

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.

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.

"Errare humanum est, sed in errare perseverare diabolicum."

This topic is closed to new replies.

Advertisement