Skip to content

Mouse Lock in Windows!

November 11, 2011

After a good amount of researching through the Firefox source, especially with the Cross Reference website I’ve found out how to get mouse lock sort-of working on Windows. The key to this was the ClipCursor function in windows.h. This takes a rectangle which says where the mouse cursor can move around in. By setting this to the windows width/2, height/2, for the left/top, and the right/bottom, you’ll have a rectangle with no width and height in the middle of the screen, with a cursor that cannot move.

Here’s my little snippet that does just that:

http://pastebin.com/gNmh1uhz

The only problem was, this doesn’t track mouse deltas, it doesn’t seem to allow for it. So I looked up the Win32 API some more on how to set the mouse position directly. I found SetMouseCursor(x,y), perfect. So my new logic was, if the mouse is not in the center, it must have moved. If it has moved, track the movement change then force it back into the center. It seems to have worked, but I still have to test it a bit more.

So I ended up with this (requires adding the mMousePos and mMouseLock member variables):

http://pastebin.com/3YBfXxM2

I have no clue if this is right, but I’m guessing it’s a step in the right direction in terms of what to do on Windows. Next time I’ll have to research more about getting the mouse lock/unlock functions visible on the DOM.

From → Open Source

One Comment

Leave a comment