SDL 3.0
SDL_mouse.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * \file SDL_mouse.h
24 *
25 * Include file for SDL mouse event handling.
26 */
27
28#ifndef SDL_mouse_h_
29#define SDL_mouse_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33#include <SDL3/SDL_video.h>
34
35#include <SDL3/SDL_begin_code.h>
36/* Set up for C function definitions, even when using C++ */
37#ifdef __cplusplus
38extern "C" {
39#endif
40
42
43typedef struct SDL_Cursor SDL_Cursor; /**< Implementation dependent */
44
45/**
46 * Cursor types for SDL_CreateSystemCursor().
47 */
48typedef enum
49{
53 SDL_SYSTEM_CURSOR_CROSSHAIR, /**< Crosshair */
54 SDL_SYSTEM_CURSOR_WAITARROW, /**< Small wait cursor (or Wait if not available) */
55 SDL_SYSTEM_CURSOR_SIZENWSE, /**< Double arrow pointing northwest and southeast */
56 SDL_SYSTEM_CURSOR_SIZENESW, /**< Double arrow pointing northeast and southwest */
57 SDL_SYSTEM_CURSOR_SIZEWE, /**< Double arrow pointing west and east */
58 SDL_SYSTEM_CURSOR_SIZENS, /**< Double arrow pointing north and south */
59 SDL_SYSTEM_CURSOR_SIZEALL, /**< Four pointed arrow pointing north, south, east, and west */
60 SDL_SYSTEM_CURSOR_NO, /**< Slashed circle or crossbones */
62 SDL_SYSTEM_CURSOR_WINDOW_TOPLEFT, /**< Window resize top-left (or SIZENWSE) */
63 SDL_SYSTEM_CURSOR_WINDOW_TOP, /**< Window resize top (or SIZENS) */
64 SDL_SYSTEM_CURSOR_WINDOW_TOPRIGHT, /**< Window resize top-right (or SIZENESW) */
65 SDL_SYSTEM_CURSOR_WINDOW_RIGHT, /**< Window resize right (or SIZEWE) */
66 SDL_SYSTEM_CURSOR_WINDOW_BOTTOMRIGHT, /**< Window resize bottom-right (or SIZENWSE) */
67 SDL_SYSTEM_CURSOR_WINDOW_BOTTOM, /**< Window resize bottom (or SIZENS) */
68 SDL_SYSTEM_CURSOR_WINDOW_BOTTOMLEFT, /**< Window resize bottom-left (or SIZENESW) */
69 SDL_SYSTEM_CURSOR_WINDOW_LEFT, /**< Window resize left (or SIZEWE) */
72
73/**
74 * Scroll direction types for the Scroll event
75 */
76typedef enum
77{
78 SDL_MOUSEWHEEL_NORMAL, /**< The scroll direction is normal */
79 SDL_MOUSEWHEEL_FLIPPED /**< The scroll direction is flipped / natural */
81
82/* Function prototypes */
83
84/**
85 * Get the window which currently has mouse focus.
86 *
87 * \returns the window with mouse focus.
88 *
89 * \since This function is available since SDL 3.0.0.
90 */
91extern DECLSPEC SDL_Window * SDLCALL SDL_GetMouseFocus(void);
92
93/**
94 * Retrieve the current state of the mouse.
95 *
96 * The current button state is returned as a button bitmask, which can be
97 * tested using the `SDL_BUTTON(X)` macros (where `X` is generally 1 for the
98 * left, 2 for middle, 3 for the right button), and `x` and `y` are set to the
99 * mouse cursor position relative to the focus window. You can pass NULL for
100 * either `x` or `y`.
101 *
102 * \param x the x coordinate of the mouse cursor position relative to the
103 * focus window
104 * \param y the y coordinate of the mouse cursor position relative to the
105 * focus window
106 * \returns a 32-bit button bitmask of the current button state.
107 *
108 * \since This function is available since SDL 3.0.0.
109 *
110 * \sa SDL_GetGlobalMouseState
111 * \sa SDL_GetRelativeMouseState
112 * \sa SDL_PumpEvents
113 */
114extern DECLSPEC Uint32 SDLCALL SDL_GetMouseState(float *x, float *y);
115
116/**
117 * Get the current state of the mouse in relation to the desktop.
118 *
119 * This works similarly to SDL_GetMouseState(), but the coordinates will be
120 * reported relative to the top-left of the desktop. This can be useful if you
121 * need to track the mouse outside of a specific window and SDL_CaptureMouse()
122 * doesn't fit your needs. For example, it could be useful if you need to
123 * track the mouse while dragging a window, where coordinates relative to a
124 * window might not be in sync at all times.
125 *
126 * Note: SDL_GetMouseState() returns the mouse position as SDL understands it
127 * from the last pump of the event queue. This function, however, queries the
128 * OS for the current mouse position, and as such, might be a slightly less
129 * efficient function. Unless you know what you're doing and have a good
130 * reason to use this function, you probably want SDL_GetMouseState() instead.
131 *
132 * \param x filled in with the current X coord relative to the desktop; can be
133 * NULL
134 * \param y filled in with the current Y coord relative to the desktop; can be
135 * NULL
136 * \returns the current button state as a bitmask which can be tested using
137 * the SDL_BUTTON(X) macros.
138 *
139 * \since This function is available since SDL 3.0.0.
140 *
141 * \sa SDL_CaptureMouse
142 */
143extern DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(float *x, float *y);
144
145/**
146 * Retrieve the relative state of the mouse.
147 *
148 * The current button state is returned as a button bitmask, which can be
149 * tested using the `SDL_BUTTON(X)` macros (where `X` is generally 1 for the
150 * left, 2 for middle, 3 for the right button), and `x` and `y` are set to the
151 * mouse deltas since the last call to SDL_GetRelativeMouseState() or since
152 * event initialization. You can pass NULL for either `x` or `y`.
153 *
154 * \param x a pointer filled with the last recorded x coordinate of the mouse
155 * \param y a pointer filled with the last recorded y coordinate of the mouse
156 * \returns a 32-bit button bitmask of the relative button state.
157 *
158 * \since This function is available since SDL 3.0.0.
159 *
160 * \sa SDL_GetMouseState
161 */
162extern DECLSPEC Uint32 SDLCALL SDL_GetRelativeMouseState(float *x, float *y);
163
164/**
165 * Move the mouse cursor to the given position within the window.
166 *
167 * This function generates a mouse motion event if relative mode is not
168 * enabled. If relative mode is enabled, you can force mouse events for the
169 * warp by setting the SDL_HINT_MOUSE_RELATIVE_WARP_MOTION hint.
170 *
171 * Note that this function will appear to succeed, but not actually move the
172 * mouse when used over Microsoft Remote Desktop.
173 *
174 * \param window the window to move the mouse into, or NULL for the current
175 * mouse focus
176 * \param x the x coordinate within the window
177 * \param y the y coordinate within the window
178 *
179 * \since This function is available since SDL 3.0.0.
180 *
181 * \sa SDL_WarpMouseGlobal
182 */
183extern DECLSPEC void SDLCALL SDL_WarpMouseInWindow(SDL_Window * window,
184 float x, float y);
185
186/**
187 * Move the mouse to the given position in global screen space.
188 *
189 * This function generates a mouse motion event.
190 *
191 * A failure of this function usually means that it is unsupported by a
192 * platform.
193 *
194 * Note that this function will appear to succeed, but not actually move the
195 * mouse when used over Microsoft Remote Desktop.
196 *
197 * \param x the x coordinate
198 * \param y the y coordinate
199 * \returns 0 on success or a negative error code on failure; call
200 * SDL_GetError() for more information.
201 *
202 * \since This function is available since SDL 3.0.0.
203 *
204 * \sa SDL_WarpMouseInWindow
205 */
206extern DECLSPEC int SDLCALL SDL_WarpMouseGlobal(float x, float y);
207
208/**
209 * Set relative mouse mode.
210 *
211 * While the mouse is in relative mode, the cursor is hidden, the mouse
212 * position is constrained to the window, and SDL will report continuous
213 * relative mouse motion even if the mouse is at the edge of the window.
214 *
215 * This function will flush any pending mouse motion.
216 *
217 * \param enabled SDL_TRUE to enable relative mode, SDL_FALSE to disable.
218 * \returns 0 on success or a negative error code on failure; call
219 * SDL_GetError() for more information.
220 *
221 * \since This function is available since SDL 3.0.0.
222 *
223 * \sa SDL_GetRelativeMouseMode
224 */
225extern DECLSPEC int SDLCALL SDL_SetRelativeMouseMode(SDL_bool enabled);
226
227/**
228 * Capture the mouse and to track input outside an SDL window.
229 *
230 * Capturing enables your app to obtain mouse events globally, instead of just
231 * within your window. Not all video targets support this function. When
232 * capturing is enabled, the current window will get all mouse events, but
233 * unlike relative mode, no change is made to the cursor and it is not
234 * restrained to your window.
235 *
236 * This function may also deny mouse input to other windows--both those in
237 * your application and others on the system--so you should use this function
238 * sparingly, and in small bursts. For example, you might want to track the
239 * mouse while the user is dragging something, until the user releases a mouse
240 * button. It is not recommended that you capture the mouse for long periods
241 * of time, such as the entire time your app is running. For that, you should
242 * probably use SDL_SetRelativeMouseMode() or SDL_SetWindowGrab(), depending
243 * on your goals.
244 *
245 * While captured, mouse events still report coordinates relative to the
246 * current (foreground) window, but those coordinates may be outside the
247 * bounds of the window (including negative values). Capturing is only allowed
248 * for the foreground window. If the window loses focus while capturing, the
249 * capture will be disabled automatically.
250 *
251 * While capturing is enabled, the current window will have the
252 * `SDL_WINDOW_MOUSE_CAPTURE` flag set.
253 *
254 * Please note that as of SDL 2.0.22, SDL will attempt to "auto capture" the
255 * mouse while the user is pressing a button; this is to try and make mouse
256 * behavior more consistent between platforms, and deal with the common case
257 * of a user dragging the mouse outside of the window. This means that if you
258 * are calling SDL_CaptureMouse() only to deal with this situation, you no
259 * longer have to (although it is safe to do so). If this causes problems for
260 * your app, you can disable auto capture by setting the
261 * `SDL_HINT_MOUSE_AUTO_CAPTURE` hint to zero.
262 *
263 * \param enabled SDL_TRUE to enable capturing, SDL_FALSE to disable.
264 * \returns 0 on success or a negative error code on failure; call
265 * SDL_GetError() for more information.
266 *
267 * \since This function is available since SDL 3.0.0.
268 *
269 * \sa SDL_GetGlobalMouseState
270 */
271extern DECLSPEC int SDLCALL SDL_CaptureMouse(SDL_bool enabled);
272
273/**
274 * Query whether relative mouse mode is enabled.
275 *
276 * \returns SDL_TRUE if relative mode is enabled or SDL_FALSE otherwise.
277 *
278 * \since This function is available since SDL 3.0.0.
279 *
280 * \sa SDL_SetRelativeMouseMode
281 */
282extern DECLSPEC SDL_bool SDLCALL SDL_GetRelativeMouseMode(void);
283
284/**
285 * Create a cursor using the specified bitmap data and mask (in MSB format).
286 *
287 * `mask` has to be in MSB (Most Significant Bit) format.
288 *
289 * The cursor width (`w`) must be a multiple of 8 bits.
290 *
291 * The cursor is created in black and white according to the following:
292 *
293 * - data=0, mask=1: white
294 * - data=1, mask=1: black
295 * - data=0, mask=0: transparent
296 * - data=1, mask=0: inverted color if possible, black if not.
297 *
298 * Cursors created with this function must be freed with SDL_DestroyCursor().
299 *
300 * If you want to have a color cursor, or create your cursor from an
301 * SDL_Surface, you should use SDL_CreateColorCursor(). Alternately, you can
302 * hide the cursor and draw your own as part of your game's rendering, but it
303 * will be bound to the framerate.
304 *
305 * Also, since SDL 2.0.0, SDL_CreateSystemCursor() is available, which
306 * provides twelve readily available system cursors to pick from.
307 *
308 * \param data the color value for each pixel of the cursor
309 * \param mask the mask value for each pixel of the cursor
310 * \param w the width of the cursor
311 * \param h the height of the cursor
312 * \param hot_x the X-axis location of the upper left corner of the cursor
313 * relative to the actual mouse position
314 * \param hot_y the Y-axis location of the upper left corner of the cursor
315 * relative to the actual mouse position
316 * \returns a new cursor with the specified parameters on success or NULL on
317 * failure; call SDL_GetError() for more information.
318 *
319 * \since This function is available since SDL 3.0.0.
320 *
321 * \sa SDL_DestroyCursor
322 * \sa SDL_SetCursor
323 */
324extern DECLSPEC SDL_Cursor *SDLCALL SDL_CreateCursor(const Uint8 * data,
325 const Uint8 * mask,
326 int w, int h, int hot_x,
327 int hot_y);
328
329/**
330 * Create a color cursor.
331 *
332 * \param surface an SDL_Surface structure representing the cursor image
333 * \param hot_x the x position of the cursor hot spot
334 * \param hot_y the y position of the cursor hot spot
335 * \returns the new cursor on success or NULL on failure; call SDL_GetError()
336 * for more information.
337 *
338 * \since This function is available since SDL 3.0.0.
339 *
340 * \sa SDL_CreateCursor
341 * \sa SDL_DestroyCursor
342 */
343extern DECLSPEC SDL_Cursor *SDLCALL SDL_CreateColorCursor(SDL_Surface *surface,
344 int hot_x,
345 int hot_y);
346
347/**
348 * Create a system cursor.
349 *
350 * \param id an SDL_SystemCursor enum value
351 * \returns a cursor on success or NULL on failure; call SDL_GetError() for
352 * more information.
353 *
354 * \since This function is available since SDL 3.0.0.
355 *
356 * \sa SDL_DestroyCursor
357 */
359
360/**
361 * Set the active cursor.
362 *
363 * This function sets the currently active cursor to the specified one. If the
364 * cursor is currently visible, the change will be immediately represented on
365 * the display. SDL_SetCursor(NULL) can be used to force cursor redraw, if
366 * this is desired for any reason.
367 *
368 * \param cursor a cursor to make active
369 * \returns 0 on success or a negative error code on failure; call
370 * SDL_GetError() for more information.
371 *
372 * \since This function is available since SDL 3.0.0.
373 *
374 * \sa SDL_CreateCursor
375 * \sa SDL_GetCursor
376 */
377extern DECLSPEC int SDLCALL SDL_SetCursor(SDL_Cursor * cursor);
378
379/**
380 * Get the active cursor.
381 *
382 * This function returns a pointer to the current cursor which is owned by the
383 * library. It is not necessary to free the cursor with SDL_DestroyCursor().
384 *
385 * \returns the active cursor or NULL if there is no mouse.
386 *
387 * \since This function is available since SDL 3.0.0.
388 *
389 * \sa SDL_SetCursor
390 */
391extern DECLSPEC SDL_Cursor *SDLCALL SDL_GetCursor(void);
392
393/**
394 * Get the default cursor.
395 *
396 * You do not have to call SDL_DestroyCursor() on the return value, but it is
397 * safe to do so.
398 *
399 * \returns the default cursor on success or NULL on failure.
400 *
401 * \since This function is available since SDL 3.0.0.
402 *
403 * \sa SDL_CreateSystemCursor
404 */
405extern DECLSPEC SDL_Cursor *SDLCALL SDL_GetDefaultCursor(void);
406
407/**
408 * Free a previously-created cursor.
409 *
410 * Use this function to free cursor resources created with SDL_CreateCursor(),
411 * SDL_CreateColorCursor() or SDL_CreateSystemCursor().
412 *
413 * \param cursor the cursor to free
414 *
415 * \since This function is available since SDL 3.0.0.
416 *
417 * \sa SDL_CreateColorCursor
418 * \sa SDL_CreateCursor
419 * \sa SDL_CreateSystemCursor
420 */
421extern DECLSPEC void SDLCALL SDL_DestroyCursor(SDL_Cursor * cursor);
422
423/**
424 * Show the cursor.
425 *
426 * \returns 0 on success or a negative error code on failure; call
427 * SDL_GetError() for more information.
428 *
429 * \since This function is available since SDL 3.0.0.
430 *
431 * \sa SDL_CursorVisible
432 * \sa SDL_HideCursor
433 */
434extern DECLSPEC int SDLCALL SDL_ShowCursor(void);
435
436/**
437 * Hide the cursor.
438 *
439 * \returns 0 on success or a negative error code on failure; call
440 * SDL_GetError() for more information.
441 *
442 * \since This function is available since SDL 3.0.0.
443 *
444 * \sa SDL_CursorVisible
445 * \sa SDL_ShowCursor
446 */
447extern DECLSPEC int SDLCALL SDL_HideCursor(void);
448
449/**
450 * Return whether the cursor is currently being shown.
451 *
452 * \returns `SDL_TRUE` if the cursor is being shown, or `SDL_FALSE` if the
453 * cursor is hidden.
454 *
455 * \since This function is available since SDL 3.0.0.
456 *
457 * \sa SDL_HideCursor
458 * \sa SDL_ShowCursor
459 */
460extern DECLSPEC SDL_bool SDLCALL SDL_CursorVisible(void);
461
462/**
463 * Used as a mask when testing buttons in buttonstate.
464 *
465 * - Button 1: Left mouse button
466 * - Button 2: Middle mouse button
467 * - Button 3: Right mouse button
468 */
469#define SDL_BUTTON(X) (1 << ((X)-1))
470#define SDL_BUTTON_LEFT 1
471#define SDL_BUTTON_MIDDLE 2
472#define SDL_BUTTON_RIGHT 3
473#define SDL_BUTTON_X1 4
474#define SDL_BUTTON_X2 5
475#define SDL_BUTTON_LMASK SDL_BUTTON(SDL_BUTTON_LEFT)
476#define SDL_BUTTON_MMASK SDL_BUTTON(SDL_BUTTON_MIDDLE)
477#define SDL_BUTTON_RMASK SDL_BUTTON(SDL_BUTTON_RIGHT)
478#define SDL_BUTTON_X1MASK SDL_BUTTON(SDL_BUTTON_X1)
479#define SDL_BUTTON_X2MASK SDL_BUTTON(SDL_BUTTON_X2)
480
481/* Ends C function definitions when using C++ */
482#ifdef __cplusplus
483}
484#endif
485#include <SDL3/SDL_close_code.h>
486
487#endif /* SDL_mouse_h_ */
void SDL_DestroyCursor(SDL_Cursor *cursor)
SDL_Cursor * SDL_GetCursor(void)
int SDL_HideCursor(void)
Uint32 SDL_GetRelativeMouseState(float *x, float *y)
Uint32 SDL_MouseID
Definition SDL_mouse.h:41
SDL_Window * SDL_GetMouseFocus(void)
int SDL_WarpMouseGlobal(float x, float y)
SDL_bool SDL_GetRelativeMouseMode(void)
SDL_SystemCursor
Definition SDL_mouse.h:49
@ SDL_SYSTEM_CURSOR_WINDOW_TOP
Definition SDL_mouse.h:63
@ SDL_SYSTEM_CURSOR_SIZENS
Definition SDL_mouse.h:58
@ SDL_SYSTEM_CURSOR_HAND
Definition SDL_mouse.h:61
@ SDL_SYSTEM_CURSOR_ARROW
Definition SDL_mouse.h:50
@ SDL_SYSTEM_CURSOR_SIZENWSE
Definition SDL_mouse.h:55
@ SDL_SYSTEM_CURSOR_WINDOW_BOTTOM
Definition SDL_mouse.h:67
@ SDL_SYSTEM_CURSOR_WINDOW_RIGHT
Definition SDL_mouse.h:65
@ SDL_SYSTEM_CURSOR_SIZENESW
Definition SDL_mouse.h:56
@ SDL_SYSTEM_CURSOR_IBEAM
Definition SDL_mouse.h:51
@ SDL_SYSTEM_CURSOR_WINDOW_TOPLEFT
Definition SDL_mouse.h:62
@ SDL_SYSTEM_CURSOR_NO
Definition SDL_mouse.h:60
@ SDL_SYSTEM_CURSOR_WAITARROW
Definition SDL_mouse.h:54
@ SDL_SYSTEM_CURSOR_SIZEALL
Definition SDL_mouse.h:59
@ SDL_SYSTEM_CURSOR_WAIT
Definition SDL_mouse.h:52
@ SDL_NUM_SYSTEM_CURSORS
Definition SDL_mouse.h:70
@ SDL_SYSTEM_CURSOR_SIZEWE
Definition SDL_mouse.h:57
@ SDL_SYSTEM_CURSOR_WINDOW_BOTTOMLEFT
Definition SDL_mouse.h:68
@ SDL_SYSTEM_CURSOR_WINDOW_LEFT
Definition SDL_mouse.h:69
@ SDL_SYSTEM_CURSOR_WINDOW_TOPRIGHT
Definition SDL_mouse.h:64
@ SDL_SYSTEM_CURSOR_CROSSHAIR
Definition SDL_mouse.h:53
@ SDL_SYSTEM_CURSOR_WINDOW_BOTTOMRIGHT
Definition SDL_mouse.h:66
SDL_bool SDL_CursorVisible(void)
Uint32 SDL_GetGlobalMouseState(float *x, float *y)
struct SDL_Cursor SDL_Cursor
Definition SDL_mouse.h:43
SDL_Cursor * SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
Uint32 SDL_GetMouseState(float *x, float *y)
void SDL_WarpMouseInWindow(SDL_Window *window, float x, float y)
SDL_Cursor * SDL_CreateCursor(const Uint8 *data, const Uint8 *mask, int w, int h, int hot_x, int hot_y)
int SDL_CaptureMouse(SDL_bool enabled)
SDL_Cursor * SDL_CreateSystemCursor(SDL_SystemCursor id)
SDL_MouseWheelDirection
Definition SDL_mouse.h:77
@ SDL_MOUSEWHEEL_NORMAL
Definition SDL_mouse.h:78
@ SDL_MOUSEWHEEL_FLIPPED
Definition SDL_mouse.h:79
int SDL_SetRelativeMouseMode(SDL_bool enabled)
int SDL_SetCursor(SDL_Cursor *cursor)
int SDL_ShowCursor(void)
SDL_Cursor * SDL_GetDefaultCursor(void)
uint8_t Uint8
Definition SDL_stdinc.h:150
int SDL_bool
Definition SDL_stdinc.h:137
uint32_t Uint32
Definition SDL_stdinc.h:174
struct SDL_Window SDL_Window
Definition SDL_video.h:137