# Hidden mouse cursor escapes window (win32)

**URL:** <https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423>\
**Category:** support\
**Created:** [April 27, 2007, 7:04pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423 "2007-04-27T19:04:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [April 27, 2007, 7:04pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/1 "2007-04-27T19:04:01Z")

</div>

**[lonesock](https://sourceforge.net/u/lonesock/)** wrote on [Friday, April 27, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#15ce):

In my simple windowed app I’m hiding the mouse cursor using glfwDisable( GLFW\_MOUSE\_CURSOR ); This is working as expected, until I ran it on a PC with SW OpenGL…muuuuch slower. At that point the mouse would often escape the window if moved too fast. I checking into the code and it seems that internally GLFW is warping the mouse back to the center of the window, but if the delta is too great (relative to the window size) then it will escape. So this could be a problem for very slow apps or very small windows.

I’m not sure about other operating systems, but I know it would be a simple matter to call ClipCursor under Windows to guarantee mouse captivity. Does this even need fixing on the other OS’s, and if so, how hard would it be to implement? I’d be happy to do the Windows patch and submit it, just let me know.

Jonathan

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [April 27, 2007, 8:35pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/2 "2007-04-27T20:35:35Z")

</div>

**[peterpp](https://sourceforge.net/u/peterpp/)** wrote on [Friday, April 27, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#5740):

I posted this bug a long time ago:

[http://sourceforge.net/tracker/index.php?func=detail&aid=1262764&group\_id=72569&atid=534938](http://sourceforge.net/tracker/index.php?func=detail&aid=1262764&group_id=72569&atid=534938)

I would be happy if somebody will fix this.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [April 27, 2007, 10:19pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/3 "2007-04-27T22:19:54Z")

</div>

**[lonesock](https://sourceforge.net/u/lonesock/)** wrote on [Friday, April 27, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#73cc):

OK, after a tiny bit of digging:

macosx\_window.c implements CGAssociateMouseAndMouseCursorPosition  
x11\_window.c implements XGrabPointer

So, of the big 3 OS’s, only Windows is missing ClipCursor. I’ll drop it in this weekend then post back, unless somebody beats me to it 😉

Jonathan

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [April 28, 2007, 8:32am UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/4 "2007-04-28T08:32:22Z")

</div>

**[kohaistyle](https://sourceforge.net/u/kohaistyle/)** wrote on [Saturday, April 28, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#e37e):

It’s be as easy as to clip the mouse yourself, with a “glfwSetMousePos( int xpos, int ypos )” call …

Or am i wrong ? Cos i doesn’t really look like a bug, but a pb of the the app ( or rather win32 callback system ) to poll the mouse position.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [April 28, 2007, 8:54am UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/5 "2007-04-28T08:54:29Z")

</div>

**[melekor](https://sourceforge.net/u/melekor/)** wrote on [Saturday, April 28, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#e37e/8fd7):

No, because once you detect it’s out of bounds, it’s already too late. ClipCursor is the correct solution.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 1, 2007, 7:15pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/6 "2007-05-01T19:15:21Z")

</div>

**[lonesock](https://sourceforge.net/u/lonesock/)** wrote on [Tuesday, May 01, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#22fe):

OK, here is my "patch" (sorry, not in any standard diff format)  
All of this applies to the CVS HEAD version of win32\_window.c

the simple functions here are shown in their entirety, just  
Copy-N-Paste over the old versions.

void \_glfwPlatformHideMouseCursor( void )  
{  
// Hide cursor  
ShowCursor( FALSE );

```
// clip cursor to the window
RECT ClipWindowRect;
if\( GetWindowRect\( \_glfwWin.Wnd, &amp;ClipWindowRect \) \)
\{
	ClipCursor\( &amp;ClipWindowRect \);
\}

// Capture cursor to user window
SetCapture\( \_glfwWin.Wnd \);

```

}

void \_glfwPlatformShowMouseCursor( void )  
{  
// Un-capture cursor  
ReleaseCapture();

```
//	unclip cursor from the window
ClipCursor\( NULL \);

// Show cursor
ShowCursor\( TRUE \);

```

}

These two work great for clipping the mouse cursor to the \_exsting\_  
window, however you need to update the clip rectangle any time the  
coordinates of the window change, so in the  
\_glfwWindowCallback  
function, I needed to modify the  
case WM\_SIZE:  
and I needed to add the  
case WM\_MOVE:

Here are the relevant snippets:

// Resize the window?  
case WM\_SIZE:  
// get the new size  
\_glfwWin.Width = LOWORD(lParam);  
\_glfwWin.Height = HIWORD(lParam);  
// if the mouse is locked, I need to update the locking coordinates  
if( \_glfwWin.MouseLock )  
{  
RECT ClipWindowRect;  
if( GetWindowRect( \_glfwWin.Wnd, &ClipWindowRect ) )  
{  
ClipCursor( &ClipWindowRect );  
}  
}  
// and call the user-supplied function, if it exists  
if( \_glfwWin.WindowSizeCallback )  
{  
\_glfwWin.WindowSizeCallback( LOWORD(lParam),  
HIWORD(lParam) );  
}  
return 0;

```
	// Move the window?
    case WM\_MOVE:
		//	if the mouse is locked, I need to update the locking coordinates
        if\( \_glfwWin.MouseLock \)
        \{
        	RECT ClipWindowRect;
			if\( GetWindowRect\( \_glfwWin.Wnd, &amp;ClipWindowRect \) \)
			\{
				ClipCursor\( &amp;ClipWindowRect \);
			\}
        \}
		//	do I return something? or just break?
		return 0;

```

Please Note that I did not know how to finish the WM\_MOVE case…sometimes there was a return 0 or 1, and sometimes a break. I defaulted to the same behavior as the WM\_SIZE case, which was to return 0, though that may be wrong…please help, somebody?

Oh, and while I’m here, one more plug for my custom ICON patch [8^)  
inside \_glfwPlatformOpenWindow()

// modifying this to load a user-specified Icon, if available  
wc.hIcon = LoadIcon( wc.hInstance, "GLFW\_ICON" ); // Load default icon  
if( !wc.hIcon )  
wc.hIcon = LoadIcon( NULL, IDI\_WINLOGO ); // Load default icon

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 1, 2007, 7:24pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/7 "2007-05-01T19:24:56Z")

</div>

**[melekor](https://sourceforge.net/u/melekor/)** wrote on [Tuesday, May 01, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#22fe/c4cd):

Nice job! I’ll definitely be adding this code to my customized version of glfw. About the return/break question, return 0 is the correct behavior.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 1, 2007, 8:58pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/8 "2007-05-01T20:58:52Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Tuesday, May 01, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#22fe/c636):

Cool, I’ll integrate this into my tree and test it. Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 1, 2007, 10:00pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/9 "2007-05-01T22:00:14Z")

</div>

**[lonesock](https://sourceforge.net/u/lonesock/)** wrote on [Tuesday, May 01, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#3633):

@melekor: thanks!

@elmindreda: you’re welcome!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 2, 2007, 11:41pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/10 "2007-05-02T23:41:03Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Wednesday, May 02, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#3633/ff1b):

I’ve caught up somewhat and now it’s time to merge this. Could you please submit a proper patch from your tree? You said that you’re working against CVS HEAD, so it should be very simple to generate a patch with whatever CVS client you’re using.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 3, 2007, 6:19am UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/11 "2007-05-03T06:19:28Z")

</div>

**[lonesock](https://sourceforge.net/u/lonesock/)** wrote on [Thursday, May 03, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#3633/ff1b/9cce):

Done! "Adds ClipCursor to win32\_window.c"

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 5, 2007, 4:55pm UTC](https://discourse.glfw.org/t/hidden-mouse-cursor-escapes-window-win32/423/12 "2007-05-05T16:55:30Z")

</div>

**[elmindreda](https://sourceforge.net/u/elmindreda/)** wrote on [Saturday, May 05, 2007](https://sourceforge.net/p/glfw/discussion/247562/thread/37bd7799/#3633/ff1b/9cce/d778):

Thank you. Merged and committed, except for the icon thingy, which I’m still considering. It’s sufficiently low impact that I’ll probably let it through, however.
