Any GUI application has to deal with the mouse, and SDL provides functions to manage mouse input and the displayed cursor.
More...
Most interactions with the mouse will come through the event subsystem. Moving a mouse generates an EVENT_MOUSE_MOTION event, pushing a button generates EVENT_MOUSE_BUTTON_DOWN, etc, but one can also query the current state of the mouse at any time with 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 Window.SetRelativeMouseMode(), 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 HideCursor() and ShowCursor(). It might be more efficient to let the system manage the cursor, if possible, using Cursor.Set() with a custom image made through Cursor.Cursor(), or perhaps just a specific system cursor from Cursor.Cursor().
SDL can, on many platforms, differentiate between multiple connected mice, allowing for interesting input scenarios and multiplayer games. They can be enumerated with GetMice(), and SDL will send EVENT_MOUSE_ADDED and 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 TOUCH_MOUSEID/PEN_MOUSEID.
◆ MouseButtonFlags
◆ MouseID
If the mouse is disconnected and reconnected, it will get a new ID.
The value 0 is an invalid ID.
- Since
- This datatype is available since SDL 3.2.0.
◆ MouseWheelDirection
- Since
- This enum is available since SDL 3.2.0.
◆ SystemCursor
- Since
- This enum is available since SDL 3.2.0.
◆ CaptureMouse()
| void SDL::CaptureMouse |
( |
bool |
enabled | ) |
|
|
inline |
Capturing enables your app to obtain mouse events globally, instead of just within your window. Not all video targets support this function. When capturing is enabled, the current window will get all mouse events, but unlike relative mode, no change is made to the cursor and it is not restrained to your window.
This function may also deny mouse input to other windows–both those in your application and others on the system–so you should use this function sparingly, and in small bursts. For example, you might want to track the mouse while the user is dragging something, until the user releases a mouse button. It is not recommended that you capture the mouse for long periods of time, such as the entire time your app is running. For that, you should probably use Window.SetRelativeMouseMode() or Window.SetMouseGrab(), depending on your goals.
While captured, mouse events still report coordinates relative to the current (foreground) window, but those coordinates may be outside the bounds of the window (including negative values). Capturing is only allowed for the foreground window. If the window loses focus while capturing, the capture will be disabled automatically.
While capturing is enabled, the current window will have the WINDOW_MOUSE_CAPTURE flag set.
Please note that SDL will attempt to "auto capture" the mouse while the user is pressing a button; this is to try and make mouse behavior more consistent between platforms, and deal with the common case of a user dragging the mouse outside of the window. This means that if you are calling CaptureMouse() only to deal with this situation, you do not have to (although it is safe to do so). If this causes problems for your app, you can disable auto capture by setting the SDL_HINT_MOUSE_AUTO_CAPTURE hint to zero.
- Parameters
-
| enabled | true to enable capturing, false to disable. |
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetGlobalMouseState
◆ CreateColorCursor()
If this function is passed a surface with alternate representations, the surface will be interpreted as the content to be used for 100% display scale, and the alternate representations will be used for high DPI situations. For example, if the original surface is 32x32, then on a 2x macOS display or 200% display scale on Windows, a 64x64 version of the image will be used, if available. If a matching version of the image isn't available, the closest larger size image will be downscaled to the appropriate size and be used instead, if available. Otherwise, the closest smaller image will be upscaled and be used instead.
- Parameters
-
| surface | an Surface structure representing the cursor image. |
| hot | the position of the cursor hot spot. |
- Returns
- the new cursor on success.
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- Cursor.Cursor
-
Cursor.Cursor
-
Cursor.Destroy
-
Cursor.Set
◆ CreateCursor()
mask has to be in MSB (Most Significant Bit) format.
The cursor width (w) must be a multiple of 8 bits.
The cursor is created in black and white according to the following:
- data=0, mask=1: white
- data=1, mask=1: black
- data=0, mask=0: transparent
- data=1, mask=0: inverted color if possible, black if not.
Cursors created with this function must be freed with Cursor.Destroy().
If you want to have a color cursor, or create your cursor from an Surface, you should use Cursor.Cursor(). Alternately, you can hide the cursor and draw your own as part of your game's rendering, but it will be bound to the framerate.
Also, Cursor.Cursor() is available, which provides several readily-available system cursors to pick from.
- Parameters
-
| data | the color value for each pixel of the cursor. |
| mask | the mask value for each pixel of the cursor. |
| size | the width and height of the cursor. |
| hot | the x position of the cursor hot spot, from the top-left, in the range of 0 to size.x - 1 and 0 to size.y - 1. |
- Returns
- a new cursor with the specified parameters on success.
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- Cursor.Cursor
-
Cursor.Cursor
-
Cursor.Destroy
-
Cursor.Set
◆ CreateSystemCursor()
- Parameters
-
| id | an SystemCursor enum value. |
- Returns
- a cursor on success.
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- Cursor.Destroy
◆ CursorVisible()
| bool SDL::CursorVisible |
( |
| ) |
|
|
inline |
- Returns
true if the cursor is being shown, or false if the cursor is hidden.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- HideCursor
-
ShowCursor
◆ Destroy()
| void SDL::Cursor::Destroy |
( |
| ) |
|
|
inline |
◆ DestroyCursor()
◆ GetCursor()
This function returns a pointer to the current cursor which is owned by the library. It is not necessary to free the cursor with Cursor.Destroy().
- Returns
- the active cursor or nullptr if there is no mouse.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- Cursor.Set
◆ GetDefaultCursor()
You do not have to call Cursor.Destroy() on the return value, but it is safe to do so.
- Returns
- the default cursor on success.
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
◆ GetGlobalMouseState()
This function immediately queries the platform for the most recent asynchronous state, more costly than retrieving SDL's cached state in GetMouseState().
Passing non-nullptr pointers to x or y will write the destination with respective x or y coordinates relative to the desktop.
In Relative Mode, the platform-cursor's position usually contradicts the SDL-cursor's position as manually calculated from GetMouseState() and Window.GetPosition.
This function can be useful if you need to track the mouse outside of a specific window and CaptureMouse() doesn't fit your needs. For example, it could be useful if you need to track the mouse while dragging a window, where coordinates relative to a window might not be in sync at all times.
- Parameters
-
| x | a pointer to receive the platform-cursor's x-position from the desktop's top left corner, can be nullptr if unused. |
| y | a pointer to receive the platform-cursor's y-position from the desktop's top left corner, can be nullptr if unused. |
- Returns
- a 32-bit bitmask of the button state that can be bitwise-compared against the ButtonMask(X) macro.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- CaptureMouse
-
GetMouseState
-
GetGlobalMouseState
◆ GetMice()
Note that this will include any device or virtual driver that includes mouse functionality, including some game controllers, KVM switches, etc. You should wait for input from a device before you consider it actively in use.
- Returns
- a 0 terminated array of mouse instance IDs.
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetMouseNameForID
-
HasMouse
◆ GetMouseFocus()
- Returns
- the window with mouse focus.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
◆ GetMouseNameForID()
| const char * SDL::GetMouseNameForID |
( |
MouseID |
instance_id | ) |
|
|
inline |
This function returns "" if the mouse doesn't have a name.
- Parameters
-
| instance_id | the mouse instance ID. |
- Returns
- the name of the selected mouse, or nullptr on failure; call GetError() for more information.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetMice
◆ GetMouseState()
This function returns the cached synchronous state as SDL understands it from the last pump of the event queue.
To query the platform for immediate asynchronous state, use GetGlobalMouseState.
Passing non-nullptr pointers to x or y will write the destination with respective x or y coordinates relative to the focused window.
In Relative Mode, the SDL-cursor's position usually contradicts the platform-cursor's position as manually calculated from GetGlobalMouseState() and Window.GetPosition.
- Parameters
-
| x | a pointer to receive the SDL-cursor's x-position from the focused window's top left corner, can be nullptr if unused. |
| y | a pointer to receive the SDL-cursor's y-position from the focused window's top left corner, can be nullptr if unused. |
- Returns
- a 32-bit bitmask of the button state that can be bitwise-compared against the ButtonMask(X) macro.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetGlobalMouseState
-
GetRelativeMouseState
◆ GetRelativeMouseMode()
| bool SDL::Window::GetRelativeMouseMode |
( |
| ) |
const |
|
inline |
- Returns
- true if relative mode is enabled for a window or false otherwise.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- Window.SetRelativeMouseMode
◆ GetRelativeMouseState()
This function returns the cached synchronous state as SDL understands it from the last pump of the event queue.
To query the platform for immediate asynchronous state, use GetGlobalMouseState.
Passing non-nullptr pointers to x or y will write the destination with respective x or y deltas accumulated since the last call to this function (or since event initialization).
This function is useful for reducing overhead by processing relative mouse inputs in one go per-frame instead of individually per-event, at the expense of losing the order between events within the frame (e.g. quickly pressing and releasing a button within the same frame).
- Parameters
-
| x | a pointer to receive the x mouse delta accumulated since last call, can be nullptr if unused. |
| y | a pointer to receive the y mouse delta accumulated since last call, can be nullptr if unused. |
- Returns
- a 32-bit bitmask of the button state that can be bitwise-compared against the ButtonMask(X) macro.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetMouseState
-
GetGlobalMouseState
◆ HasMouse()
- Returns
- true if a mouse is connected, false otherwise.
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetMice
◆ HideCursor()
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- CursorVisible
-
ShowCursor
◆ Set()
| void SDL::Cursor::Set |
( |
| ) |
|
|
inline |
This function sets the currently active cursor to the specified one. If the cursor is currently visible, the change will be immediately represented on the display. Cursor.Set(nullptr) can be used to force cursor redraw, if this is desired for any reason.
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetCursor
◆ SetCursor()
This function sets the currently active cursor to the specified one. If the cursor is currently visible, the change will be immediately represented on the display. Cursor.Set(nullptr) can be used to force cursor redraw, if this is desired for any reason.
- Parameters
-
| cursor | a cursor to make active. |
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- GetCursor
◆ SetRelativeMouseMode()
| void SDL::Window::SetRelativeMouseMode |
( |
bool |
enabled | ) |
|
|
inline |
While the window has focus and relative mouse mode is enabled, the cursor is hidden, the mouse position is constrained to the window, and SDL will report continuous relative mouse motion even if the mouse is at the edge of the window.
If you'd like to keep the mouse position fixed while in relative mode you can use Window.SetMouseRect(). If you'd like the cursor to be at a specific location when relative mode ends, you should use Window.WarpMouse() before disabling relative mode.
This function will flush any pending mouse motion for this window.
- Parameters
-
| enabled | true to enable relative mode, false to disable. |
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- Window.GetRelativeMouseMode
◆ ShowCursor()
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- CursorVisible
-
HideCursor
◆ WarpMouse() [1/2]
This function generates a mouse motion event.
A failure of this function usually means that it is unsupported by a platform.
Note that this function will appear to succeed, but not actually move the mouse when used over Microsoft Remote Desktop.
- Parameters
-
- Exceptions
-
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- Window.WarpMouse
◆ WarpMouse() [2/2]
| void SDL::Window::WarpMouse |
( |
const FPointRaw & |
p | ) |
|
|
inline |
This function generates a mouse motion event if relative mode is not enabled. If relative mode is enabled, you can force mouse events for the warp by setting the SDL_HINT_MOUSE_RELATIVE_WARP_MOTION hint.
Note that this function will appear to succeed, but not actually move the mouse when used over Microsoft Remote Desktop.
- Parameters
-
| p | the x, y coordinates within the window. |
- Thread safety:
- This function should only be called on the main thread.
- Since
- This function is available since SDL 3.2.0.
- See also
- WarpMouse
◆ MOUSEWHEEL_FLIPPED
◆ MOUSEWHEEL_NORMAL
◆ SYSTEM_CURSOR_CROSSHAIR
Initial value:=
SDL_SYSTEM_CURSOR_CROSSHAIR
◆ SYSTEM_CURSOR_DEFAULT
Initial value:=
SDL_SYSTEM_CURSOR_DEFAULT
◆ SYSTEM_CURSOR_E_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_E_RESIZE
◆ SYSTEM_CURSOR_EW_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_EW_RESIZE
◆ SYSTEM_CURSOR_N_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_N_RESIZE
◆ SYSTEM_CURSOR_NE_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_NE_RESIZE
May be NESW_RESIZE.
◆ SYSTEM_CURSOR_NESW_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_NESW_RESIZE
◆ SYSTEM_CURSOR_NOT_ALLOWED
Initial value:=
SDL_SYSTEM_CURSOR_NOT_ALLOWED
◆ SYSTEM_CURSOR_NS_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_NS_RESIZE
◆ SYSTEM_CURSOR_NW_RESIZE
| constexpr SystemCursor SDL::SYSTEM_CURSOR_NW_RESIZE = SDL_SYSTEM_CURSOR_NW_RESIZE |
|
constexpr |
This may be a single arrow or a double arrow like NWSE_RESIZE.
◆ SYSTEM_CURSOR_NWSE_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_NWSE_RESIZE
◆ SYSTEM_CURSOR_S_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_S_RESIZE
◆ SYSTEM_CURSOR_SE_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_SE_RESIZE
May be NWSE_RESIZE.
◆ SYSTEM_CURSOR_SW_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_SW_RESIZE
May be NESW_RESIZE.
◆ SYSTEM_CURSOR_TEXT
◆ SYSTEM_CURSOR_W_RESIZE
Initial value:=
SDL_SYSTEM_CURSOR_W_RESIZE