Locking cursor to window without hiding it?


When I tab out of my GLFW window, my first person camera flies around all over the place. I know you can stop this with glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); but that means that I can no longer see my crosshair cursor (which I need). Can I lock the cursor to the screen without hiding it?


I think there might be two separate issues here.

One is that your camera goes wild when you tab out of the window, the other is that you want a method to constrain the cursor to the window without hiding it.

To constrain the cursor you could disable the cursor and draw your own - this is the approach used by most games, see this post on some approaches to drawing your own cursor.

Another approach might be to constrain the cursor by calling glfwSetCursorPos when it reaches the outer edge to keep it inside, though there are likely a few problems in getting this to work well.

Neither of these will help with ALT+TAB issues, as the window looses focus then. You’ll need to handle this by not updating the first person camera with mouse deltas when the window is loosing/gaining focus and also beware that minimizing the window causes a window size message of 0 width and height (which can also cause your camera to go crazy if you process these for control).


Using glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); stops the issue, even when tabbing out, so I think your first reply is probably the best option. It’s just that the side effect is no longer being able to see the crosshair.

I thought maybe there was a way to disable the cursor, but still show GLFW’s given crosshair cursor, or maybe a separate method that only locks the cursor and doesn’t hide it.

Appreciate the help though.