mouse topic
CategoryMouse
Any GUI application has to deal with the mouse, and SDL provides functions to manage mouse input and the displayed cursor.
Most interactions with the mouse will come through the event subsystem. Moving a mouse generates an SDL_EVENT_MOUSE_MOTION event, pushing a button generates SDL_EVENT_MOUSE_BUTTON_DOWN, etc, but one can also query the current state of the mouse at any time with SDL_GetMouseState().
For certain games, it's useful to disassociate the mouse cursor from mouse input. An FPS, for example, would not want the player's motion to stop as the mouse hits the edge of the window. For these scenarios, use SDL_SetWindowRelativeMouseMode(), which hides the cursor, grabs mouse input to the window, and reads mouse input no matter how far it moves.
Games that want the system to track the mouse but want to draw their own cursor can use SDL_HideCursor() and SDL_ShowCursor(). It might be more efficient to let the system manage the cursor, if possible, using SDL_SetCursor() with a custom image made through SDL_CreateColorCursor(), or perhaps just a specific system cursor from SDL_CreateSystemCursor().
SDL can, on many platforms, differentiate between multiple connected mice, allowing for interesting input scenarios and multiplayer games. They can be enumerated with SDL_GetMice(), and SDL will send SDL_EVENT_MOUSE_ADDED and SDL_EVENT_MOUSE_REMOVED events as they are connected and unplugged.
Since many apps only care about basic mouse input, SDL offers a virtual
mouse device for touch and pen input, which often can make a desktop
application work on a touchscreen phone without any code changes. Apps that
care about touch/pen separately from mouse input should filter out events
with a which
field of SDL_TOUCH_MOUSEID/SDL_PEN_MOUSEID.
Functions
-
sdlCaptureMouse(
bool enabled) → bool mouse - Capture the mouse and to track input outside an SDL window.
-
sdlCreateColorCursor(
Pointer< mouseSdlSurface> surface, int hotX, int hotY) → Pointer<SdlCursor> - Create a color cursor.
-
sdlCreateCursor(
Pointer< mouseUint8> data, Pointer<Uint8> mask, int w, int h, int hotX, int hotY) → Pointer<SdlCursor> - Create a cursor using the specified bitmap data and mask (in MSB format).
-
sdlCreateSystemCursor(
int id) → Pointer< mouseSdlCursor> - Create a system cursor.
-
sdlCursorVisible(
) → bool mouse - Return whether the cursor is currently being shown.
-
sdlDestroyCursor(
Pointer< mouseSdlCursor> cursor) → void - Free a previously-created cursor.
-
sdlGetCursor(
) → Pointer< mouseSdlCursor> - Get the active cursor.
-
sdlGetDefaultCursor(
) → Pointer< mouseSdlCursor> - Get the default cursor.
-
sdlGetGlobalMouseState(
Pointer< mouseFloat> x, Pointer<Float> y) → int - Query the platform for the asynchronous mouse button state and the desktop-relative platform-cursor position.
-
sdlGetMice(
Pointer< mouseInt32> count) → Pointer<Uint32> - Get a list of currently connected mice.
-
sdlGetMouseFocus(
) → Pointer< mouseSdlWindow> - Get the window which currently has mouse focus.
-
sdlGetMouseNameForId(
int instanceId) → String? mouse - Get the name of a mouse.
-
sdlGetMouseState(
Pointer< mouseFloat> x, Pointer<Float> y) → int - Query SDL's cache for the synchronous mouse button state and the window-relative SDL-cursor position.
-
sdlGetRelativeMouseState(
Pointer< mouseFloat> x, Pointer<Float> y) → int - Query SDL's cache for the synchronous mouse button state and accumulated mouse delta since last call.
-
sdlGetWindowRelativeMouseMode(
Pointer< mouseSdlWindow> window) → bool - Query whether relative mouse mode is enabled for a window.
-
sdlHasMouse(
) → bool mouse - Return whether a mouse is currently connected.
-
sdlHideCursor(
) → bool mouse - Hide the cursor.
-
sdlSetCursor(
Pointer< mouseSdlCursor> cursor) → bool - Set the active cursor.
-
sdlSetRelativeMouseTransform(
Pointer< mouseNativeFunction< callback, Pointer<SdlMouseMotionTransformCallback> >NativeType> userdata) → bool - Set a user-defined function by which to transform relative mouse inputs.
-
sdlSetWindowRelativeMouseMode(
Pointer< mouseSdlWindow> window, bool enabled) → bool - Set relative mouse mode for a window.
-
sdlShowCursor(
) → bool mouse - Show the cursor.
-
sdlWarpMouseGlobal(
double x, double y) → bool mouse - Move the mouse to the given position in global screen space.
-
sdlWarpMouseInWindow(
Pointer< mouseSdlWindow> window, double x, double y) → void - Move the mouse cursor to the given position within the window.