14 Events
SDL3 communicates user input and system notifications through events. The safe API provides transparent Racket structs that work seamlessly with match.
14.1 Polling Events
(for ([ev (in-events)]) (match ev [(quit-event) (exit)] [(key-event 'down 'escape _ _ _) (exit)] [_ (void)]))
procedure
(poll-event) → (or/c sdl-event? #f)
procedure
(wait-event) → sdl-event?
procedure
(wait-event-timeout timeout) → (or/c sdl-event? #f)
timeout : exact-nonnegative-integer?
procedure
(should-quit?) → boolean?
14.2 Event Types
All event structs are transparent and can be used with match. Field names follow a consistent pattern for easy pattern matching.
14.2.1 Application Events
struct
(struct quit-event () #:transparent)
(match ev [(quit-event) (set! running? #f)] ...)
14.2.2 Window Events
struct
(struct window-event (type) #:transparent) type : symbol?
'close-requested —
User requested window close 'shown —
Window became visible 'hidden —
Window was hidden 'exposed —
Window needs redrawing 'moved —
Window was moved 'resized —
Window was resized 'minimized —
Window was minimized 'maximized —
Window was maximized 'restored —
Window was restored 'focus-gained —
Window gained keyboard focus 'focus-lost —
Window lost keyboard focus 'mouse-enter —
Mouse entered window 'mouse-leave —
Mouse left window
(match ev [(window-event 'close-requested) (set! running? #f)] [(window-event 'resized) (handle-resize)] ...)
14.2.3 Keyboard Events
struct
(struct key-event (type key scancode mod repeat?) #:transparent) type : (or/c 'down 'up) key : symbol? scancode : exact-nonnegative-integer? mod : exact-nonnegative-integer? repeat? : boolean?
type —
'down for key press, 'up for release key —
Key symbol (e.g., 'a, 'space, 'escape) scancode —
Physical key code (keyboard-layout independent) mod —
Modifier key bitmask (use mod-shift?, etc.) repeat? —
#t if this is a key repeat event
Common key symbols include: 'a through 'z, '0 through '9, 'space, 'escape, 'return, 'backspace, 'tab, 'up, 'down, 'left, 'right, 'f1 through 'f12.
(match ev ;; Simple key matching [(key-event 'down 'escape _ _ _) (exit)] [(key-event 'down 'space _ _ _) (fire-bullet!)] ;; With modifiers [(key-event 'down 's _ mod _) #:when (mod-ctrl? mod) (save-file!)] ;; Ignore key repeats [(key-event 'down 'w _ _ #f) (start-moving)] [(key-event 'up 'w _ _ _) (stop-moving)] ...)
(key-name 'escape) ; => "Escape" (key-name 'space) ; => "Space"
14.2.3.1 Modifier Key Predicates
These predicates check if modifier keys are held in a key-event’s mod field:
procedure
(mod-shift? mod) → boolean?
mod : exact-nonnegative-integer?
procedure
mod : exact-nonnegative-integer?
procedure
mod : exact-nonnegative-integer?
procedure
mod : exact-nonnegative-integer?
14.2.4 Text Input Events
struct
(struct text-input-event (text) #:transparent) text : string?
Must be enabled with start-text-input! first.
(match ev [(text-input-event txt) (set! input-string (string-append input-string txt))] ...)
14.2.5 Mouse Events
struct
(struct mouse-motion-event (x y xrel yrel state) #:transparent) x : real? y : real? xrel : real? yrel : real? state : exact-nonnegative-integer?
x, y —
Current mouse position xrel, yrel —
Relative motion since last event state —
Button state bitmask
struct
(struct mouse-button-event (type button x y clicks) #:transparent) type : (or/c 'down 'up) button : (or/c 'left 'middle 'right symbol?) x : real? y : real? clicks : exact-nonnegative-integer?
type —
'down or 'up button —
'left, 'middle, 'right, or a number for extra buttons x, y —
Mouse position clicks —
Click count (1 for single, 2 for double-click, etc.)
(match ev [(mouse-button-event 'down 'left x y 1) (handle-click x y)] [(mouse-button-event 'down 'left x y 2) (handle-double-click x y)] ...)
struct
(struct mouse-wheel-event (x y direction mouse-x mouse-y) #:transparent) x : real? y : real? direction : symbol? mouse-x : real? mouse-y : real?
x, y —
Scroll amounts (y is typically vertical scroll) direction —
'normal or 'flipped mouse-x, mouse-y —
Mouse position
14.2.6 Gamepad Events
struct
(struct gamepad-button-event (type which button) #:transparent) type : (or/c 'down 'up) which : exact-nonnegative-integer? button : symbol?
type —
'down or 'up which —
Gamepad instance ID button —
Button symbol (e.g., 'a, 'b, 'start)
struct
(struct gamepad-axis-event (which axis value) #:transparent) which : exact-nonnegative-integer? axis : symbol? value : real?
which —
Gamepad instance ID axis —
Axis symbol (e.g., 'leftx, 'lefty) value —
Axis value (-1.0 to 1.0)
struct
(struct gamepad-device-event (type which) #:transparent) type : (or/c 'added 'removed) which : exact-nonnegative-integer?
14.2.7 Joystick Events
struct
(struct joy-axis-event (which axis value) #:transparent) which : exact-nonnegative-integer? axis : exact-nonnegative-integer? value : real?
struct
(struct joy-button-event (type which button) #:transparent) type : (or/c 'down 'up) which : exact-nonnegative-integer? button : exact-nonnegative-integer?
struct
(struct joy-hat-event (which hat value) #:transparent) which : exact-nonnegative-integer? hat : exact-nonnegative-integer? value : exact-nonnegative-integer?
struct
(struct joy-device-event (type which) #:transparent) type : (or/c 'added 'removed) which : exact-nonnegative-integer?
14.2.8 Touch Events
struct
(struct touch-finger-event ( type touch-id finger-id x y dx dy pressure) #:transparent) type : (or/c 'down 'up 'motion) touch-id : exact-nonnegative-integer? finger-id : exact-nonnegative-integer? x : real? y : real? dx : real? dy : real? pressure : real?
x, y —
Normalized position (0.0 to 1.0) dx, dy —
Normalized motion delta pressure —
Touch pressure (0.0 to 1.0)
14.2.9 Drop Events
struct
(struct drop-event (type x y source data) #:transparent) type : (or/c 'file 'text 'begin 'complete 'position) x : real? y : real? source : string? data : (or/c string? #f)
For 'file type, data contains the file path. For 'text type, data contains the dropped text.
14.2.10 Clipboard Events
struct
(struct clipboard-event (owner? mime-types) #:transparent) owner? : boolean? mime-types : (listof string?)
14.2.11 Audio/Camera Device Events
struct
(struct audio-device-event (type which recording?) #:transparent) type : (or/c 'added 'removed) which : exact-nonnegative-integer? recording? : boolean?
struct
(struct camera-device-event (type which) #:transparent) type : (or/c 'added 'removed 'approved 'denied) which : exact-nonnegative-integer?
14.2.12 Unknown Events
struct
(struct unknown-event (type) #:transparent) type : exact-nonnegative-integer?
14.3 Event Handling Patterns
14.3.1 Basic Event Loop
(let loop () (define quit? (for/or ([ev (in-events)]) (match ev [(quit-event) #t] [(key-event 'down 'escape _ _ _) #t] [_ #f]))) (unless quit? ;; Update and render (render-frame!) (loop)))
14.3.2 Event-Driven vs State Polling
Discrete actions (menu selection, firing a weapon)
Text input
Detecting specific key presses
Smooth continuous movement
Checking if a key is currently held
Many games use both: events for actions, polling for movement.