Module mouse
dep_sdl3
only.Expand description
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.
Structs§
- SDL_
Cursor - The structure used to identify an SDL cursor.
- SDL_
Mouse Wheel Direction - Scroll direction types for the Scroll event
- SDL_
System Cursor - Cursor types for
SDL_CreateSystemCursor()
.
Constants§
- SDL_
BUTTON_ LEFT - SDL_
BUTTON_ LMASK - SDL_
BUTTON_ MIDDLE - SDL_
BUTTON_ MMASK - SDL_
BUTTON_ RIGHT - SDL_
BUTTON_ RMASK - SDL_
BUTTON_ X1 - SDL_
BUTTON_ X2 - SDL_
BUTTON_ X1MASK - SDL_
BUTTON_ X2MASK - SDL_
MOUSEWHEEL_ FLIPPED - The scroll direction is flipped / natural
- SDL_
MOUSEWHEEL_ NORMAL - The scroll direction is normal
- SDL_
SYSTEM_ CURSOR_ COUNT - SDL_
SYSTEM_ CURSOR_ CROSSHAIR - Crosshair.
- SDL_
SYSTEM_ CURSOR_ DEFAULT - Default cursor. Usually an arrow.
- SDL_
SYSTEM_ CURSOR_ EW_ RESIZE - Double arrow pointing west and east.
- SDL_
SYSTEM_ CURSOR_ E_ RESIZE - Window resize right. May be EW_RESIZE.
- SDL_
SYSTEM_ CURSOR_ MOVE - Four pointed arrow pointing north, south, east, and west.
- SDL_
SYSTEM_ CURSOR_ NESW_ RESIZE - Double arrow pointing northeast and southwest.
- SDL_
SYSTEM_ CURSOR_ NE_ RESIZE - Window resize top-right. May be NESW_RESIZE.
- SDL_
SYSTEM_ CURSOR_ NOT_ ALLOWED - Not permitted. Usually a slashed circle or crossbones.
- SDL_
SYSTEM_ CURSOR_ NS_ RESIZE - Double arrow pointing north and south.
- SDL_
SYSTEM_ CURSOR_ NWSE_ RESIZE - Double arrow pointing northwest and southeast.
- SDL_
SYSTEM_ CURSOR_ NW_ RESIZE - Window resize top-left. This may be a single arrow or a double arrow like NWSE_RESIZE.
- SDL_
SYSTEM_ CURSOR_ N_ RESIZE - Window resize top. May be NS_RESIZE.
- SDL_
SYSTEM_ CURSOR_ POINTER - Pointer that indicates a link. Usually a pointing hand.
- SDL_
SYSTEM_ CURSOR_ PROGRESS - Program is busy but still interactive. Usually it’s WAIT with an arrow.
- SDL_
SYSTEM_ CURSOR_ SE_ RESIZE - Window resize bottom-right. May be NWSE_RESIZE.
- SDL_
SYSTEM_ CURSOR_ SW_ RESIZE - Window resize bottom-left. May be NESW_RESIZE.
- SDL_
SYSTEM_ CURSOR_ S_ RESIZE - Window resize bottom. May be NS_RESIZE.
- SDL_
SYSTEM_ CURSOR_ TEXT - Text selection. Usually an I-beam.
- SDL_
SYSTEM_ CURSOR_ WAIT - Wait. Usually an hourglass or watch or spinning ball.
- SDL_
SYSTEM_ CURSOR_ W_ RESIZE - Window resize left. May be EW_RESIZE.
Functions§
- SDL_
BUTTON_ MASK - SDL_
Capture ⚠Mouse - Capture the mouse and to track input outside an SDL window.
- SDL_
Create ⚠Color Cursor - Create a color cursor.
- SDL_
Create ⚠Cursor - Create a cursor using the specified bitmap data and mask (in MSB format).
- SDL_
Create ⚠System Cursor - Create a system cursor.
- SDL_
Cursor ⚠Visible - Return whether the cursor is currently being shown.
- SDL_
Destroy ⚠Cursor - Free a previously-created cursor.
- SDL_
GetCursor ⚠ - Get the active cursor.
- SDL_
GetDefault ⚠Cursor - Get the default cursor.
- SDL_
GetGlobal ⚠Mouse State - Query the platform for the asynchronous mouse button state and the desktop-relative platform-cursor position.
- SDL_
GetMice ⚠ - Get a list of currently connected mice.
- SDL_
GetMouse ⚠Focus - Get the window which currently has mouse focus.
- SDL_
GetMouse ⚠Name ForID - Get the name of a mouse.
- SDL_
GetMouse ⚠State - Query SDL’s cache for the synchronous mouse button state and the window-relative SDL-cursor position.
- SDL_
GetRelative ⚠Mouse State - Query SDL’s cache for the synchronous mouse button state and accumulated mouse delta since last call.
- SDL_
GetWindow ⚠Relative Mouse Mode - Query whether relative mouse mode is enabled for a window.
- SDL_
HasMouse ⚠ - Return whether a mouse is currently connected.
- SDL_
Hide ⚠Cursor - Hide the cursor.
- SDL_
SetCursor ⚠ - Set the active cursor.
- SDL_
SetWindow ⚠Relative Mouse Mode - Set relative mouse mode for a window.
- SDL_
Show ⚠Cursor - Show the cursor.
- SDL_
Warp ⚠Mouse Global - Move the mouse to the given position in global screen space.
- SDL_
Warp ⚠Mouse InWindow - Move the mouse cursor to the given position within the window.
Type Aliases§
- SDL_
Mouse Button Flags - A bitmask of pressed mouse buttons, as reported by
SDL_GetMouseState
, etc. - SDL_
MouseID - This is a unique ID for a mouse for the time it is connected to the system, and is never reused for the lifetime of the application.