Windows Mouse Handling Sucks

Started by
2 comments, last by blueshogun96 6 years, 8 months ago

In my opinion, of course...

My game not only needs to know if the mouse is currently out of the game's window, but also the position of the cursor relative to the window as it moves outside.  MacOS and Linux give me this by default, but for Windows, I had quite a bit of trouble finding a reliable way to do it.  This isn't the first time I've had issues dealing with Window's mousing handling.  It's been a pain in my arse multiple projects.

 


#if defined(_WIN32) && !defined(_WINRT)
	/* Because Windows mouse handling sucks in general compared to Mac/Linux, a little extra
	   work is required to find out if the cursor is outside of the game's window to prevent
	   cheating.  In my experience, the reliability of GetCursorPos and ScreenToClient vary
	   from one comp/OS to another, so a better idea is to check every frame and use those
	   resulting coordinates only if it tells us we are off screen.  No disrespect Microsoft,
	   but why do you insist on making such trivial things a pain in my behind sometimes? */

	struct vec2_t<float> bounds;

	This->get_bounds( bounds );

	POINT pt;
	GetCursorPos((POINT*)&pt);
	ScreenToClient(GetActiveWindow(), (POINT*)&pt);

	if( pt.x < 0 || pt.x > bounds.v[0] || pt.y < 0 || pt.y > bounds.v[1] )
	{
		This->mouse_move_func(pt.x, pt.y);
	}
#endif

 

So yeah, I can't believe it took me that long to get this stupid thing figured out.  Anyone else have the same problem before?  Anyone else come up with a better solution?  I'm just glad it works....

Shogun

Advertisement

There's been a solution for this exact use case built into Windows since it was a 16-bit cooperatively scheduled layer on top of MS-DOS. The relevant starting point is the SetCapture API.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Wow, I did not know that.  Heh, learn something new every day!

Shogun

This topic is closed to new replies.

Advertisement