Showing and hiding a cursor at runtime

When I create my initial window, if I want to hide the system cursor I call the following:

glfwSetInputMode(this->glfwWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetInputMode(this->glfwWindow, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);

If I want the cursor to display, I simply do not call the above code. This works fine. However when I attempt to do this at runtime, for example, start a window and display a cursor (not calling the above code on window creation), and then call the above code at a later time (for example a main menu system for a game displays cursor so user can select which map to load, then once selection is made cursor should no longer be visible), the cursor disappears as expected when over the window region, but does not lock to the window (if cursor moved past window region it displays) and glfwGetCursorPos() does not update the cursor position.

If I click on something outside of the window (say a black space on my desktop) and then click back into the window, the mouse cursor is hidden, locked, updating position, and working as expected.

I feel like I may be doing something incorrectly in regards to swapping between displaying and not displaying the cursor…possibly?

As always, any assistance is greatly appreciated! Thanks :slight_smile:

You only need to set the GLFW_RAW_MOUSE_MOTION mode once per window, and then enable/disable GLFW_CURSOR as needed. I do this and it works fine in my tests.

By the way, what operating system are you using for this, and what version of GLFW?

Changed code to only enable GLFW_RAW_MOUSE_MOTION once per window, but issue persisting. Will see if I can get a screen capture uploaded to better demonstrate what’s going on.

OS: Windows 10 Pro 64-bit
GLFW: Your multi-context-windows branch

Uploaded a video to youtube that demonstrates the issue.

Ah! I see what’s happening now, and can replicate.

When the problem occurs your application does not have focus, so Windows controls the enter/exit of the mouse. This is an OS feature rather than a GLFW issue.

If you want to override the focus, you can call glfwFocusWindow, however doing so may not be user friendly. More information can be found in the GLFW documentation on window focus.

I wired up glfwSetWindowFocusCallback and oddly, my focus was never lost. BUT, while doing this I noticed that I was making the calls to enable/disable the mouse cursor from a secondary thread (a thread which glfw was not initialized on). On a whim I figured I would move those calls back to the primary thread and see if that took care of it…and it did.

Therefore, is it safe to assume that calls to glfwSetInputMode should always be made on the same thread which initialized the glfw window?

Glad you have this resolved!

Yes, as per the glfwSetInputMode documentation the function must only be called from the main thread.

I see you mention a thread which glfw was not initialized on. Do note that most GLFW functions must be called from the main thread (thread which calls main() ) . Some OSes do not allow any other thread to handle window callbacks and certain creation calls.

1 Like

Thank you for the clarification! :slight_smile: