Wrapping mouse to center of window, while still receiving mouse events


Was wondering if there is a function in SDL which locks the mouse cursor to the center of a window, while still receiving mouse motion events? I’ve tried using SDL_SetRelativeMouseMode(SDL_TRUE), but that doesn’t seem to achieve it. There is a function in glfw which I’m trying to replace with an SDL equivalent, which does this, called glfwSetCursor. It hides the cursor (when GLFW_CURSOR_DISABLED flag is used), but locks it to the screen center, while still receiving events. Any ideas of a similar function to this in SDL?

[EDIT: seemed to be an issue in my code. Everything now works as intended]

How is the behavior of SDL_SetRelativeMouseMode(SDL_TRUE) different from what you want?

I think it should do pretty much what you’re describing?

I thought glfwSetCursor() simply set the appearance of the cursor, rather than its position. SDL2 has SDL_SetCursor() to change its appearance and SDL_WarpMouseInWindow() to move it, but they are quite separate. There’s also SDL_ShowCursor() to hide it.

It seems to me that it’s not being centered, because if I mouse the mouse outside the window, I don’t seem to get any motion events when the mouse is moved

With relative mouse mode you shouldn’t be able to move the mouse outside of the window

What operating system and so on are you using?

Yeah, it sets the appearance, but it also “locks” is to the center, while still receiving motion events when the cursor is moved. I’m basically trying to force the cursor to stay centrally, but still allow mouse events to be registered, even when the cursor leave the window. This way, the mouse will always generate callbacks regardless of window or display extents (as it’s not being constrained inside a window, it’s essentially infinite)

What operating system and so on are you using?

I’m on manjaro, and using the v2.0.20 zip release

Wayland or X11? I think on Wayland you can’t get mouse events from outside the window.

The idea of relative mouse mode is that:

  1. The SDL window has focus
  2. The cursor is hidden
  3. The cursor won’t leave the window
  4. You only get/use relative mouse events (event.motion.xrel and .yrel)

Ah sorry, seems like it was a bug or something in my code. You’re right, it’s working as intended (on X11). Sorry for the bother!

1 Like