16 Mouse Functions
This section covers mouse input handling, including mouse state polling, cursor control, and relative mouse mode for FPS-style input.
16.1 Mouse State
procedure
(get-mouse-state) →
real? real? exact-nonnegative-integer?
x —
The x coordinate relative to the window y —
The y coordinate relative to the window buttons —
A bitmask of pressed buttons
Use mouse-button-pressed? to check individual buttons:
(define-values (x y buttons) (get-mouse-state)) (when (mouse-button-pressed? buttons SDL_BUTTON_LMASK) (printf "Left button pressed at (~a, ~a)~n" x y))
procedure
→
real? real? exact-nonnegative-integer?
x —
The x coordinate in screen space y —
The y coordinate in screen space buttons —
A bitmask of pressed buttons
procedure
→
real? real? exact-nonnegative-integer?
dx —
Relative motion in x direction dy —
Relative motion in y direction buttons —
A bitmask of pressed buttons
Useful for implementing mouse look in games.
procedure
(mouse-button-pressed? mask button) → boolean?
mask : exact-nonnegative-integer? button : exact-nonnegative-integer?
SDL_BUTTON_LMASK —
Left mouse button SDL_BUTTON_MMASK —
Middle mouse button SDL_BUTTON_RMASK —
Right mouse button SDL_BUTTON_X1MASK —
Extra button 1 SDL_BUTTON_X2MASK —
Extra button 2
(define-values (x y buttons) (get-mouse-state)) (when (mouse-button-pressed? buttons SDL_BUTTON_LMASK) (handle-left-click x y)) (when (mouse-button-pressed? buttons SDL_BUTTON_RMASK) (handle-right-click x y))
16.2 Mouse Enumeration
procedure
(has-mouse?) → boolean?
procedure
procedure
procedure
(get-mouse-name-for-id id) → (or/c string? #f)
id : exact-nonnegative-integer?
procedure
(get-mouse-focus) → (or/c cpointer? #f)
16.3 Mouse Warping
If win is #f, uses the currently focused window.
;; Move cursor to center of window (warp-mouse! win 400 300)
procedure
(warp-mouse-global! x y) → void?
x : real? y : real?
Note: This may not be supported on all platforms.
16.4 Mouse Capture
procedure
(capture-mouse! enabled?) → void?
enabled? : boolean?
When enabled, the window receives mouse events even when the cursor is outside the window. This is useful for drag operations where you want to track the mouse even if it leaves the window.
;; Start a drag operation (capture-mouse! #t) ;; ... handle drag ... ;; End the drag (capture-mouse! #f)
16.5 Relative Mouse Mode
Relative mouse mode is used for FPS-style input where the cursor is hidden and mouse motion reports relative deltas rather than absolute positions.
procedure
(set-relative-mouse-mode! win on?) → void?
win : window? on? : boolean?
The cursor is hidden
Mouse motion reports relative deltas instead of absolute positions
The cursor is confined to the window
;; Enable FPS-style mouse look (set-relative-mouse-mode! win #t) ;; Handle mouse motion events for camera control ;; motion-event-xrel and motion-event-yrel give deltas
procedure
(relative-mouse-mode? win) → boolean?
win : window?
16.6 Cursor Visibility
procedure
(show-cursor!) → void?
procedure
(hide-cursor!) → void?
procedure
16.7 System Cursors
SDL provides standard system cursors that you can use instead of the default arrow cursor.
procedure
(create-system-cursor cursor-type) → cpointer?
cursor-type : (or/c symbol? exact-nonnegative-integer?)
'default or 'arrow —
Standard arrow cursor 'text or 'ibeam —
Text editing cursor 'wait or 'hourglass —
Wait/busy cursor 'crosshair —
Crosshair cursor 'progress —
Progress indicator 'pointer or 'hand —
Pointing hand (for links) 'move —
Move/drag cursor 'not-allowed or 'no —
Not allowed cursor 'ew-resize —
Horizontal resize 'ns-resize —
Vertical resize 'nwse-resize —
Diagonal resize (NW-SE) 'nesw-resize —
Diagonal resize (NE-SW) 'n-resize, 'e-resize, etc. —
Edge resize cursors
The returned cursor must be freed with destroy-cursor! when no longer needed.
procedure
(set-cursor! cursor) → void?
cursor : (or/c cpointer? #f)
procedure
(destroy-cursor! cursor) → void?
cursor : cpointer?
syntax
(with-system-cursor cursor-type body ...)
;; Show crosshair while aiming (with-system-cursor 'crosshair (handle-aiming)) ;; Show pointer when hovering over a button (when (mouse-over-button? x y) (with-system-cursor 'pointer (draw-button-hover)))
16.8 Cursor Type Conversion
procedure
sym : symbol?
procedure
(system-cursor->symbol id) → symbol?
id : exact-nonnegative-integer?
16.9 Mouse Button Constants
These constants identify individual mouse buttons:
These constants are bitmasks for checking button state: