15 Keyboard Functions
This section covers keyboard input handling, including key state polling, modifier detection, and text input.
15.1 Keyboard State
procedure
(key-pressed? key) → boolean?
key : (or/c symbol? exact-nonnegative-integer?)
A symbol like 'escape, 'space, 'w, 'left-shift
A raw SDL scancode constant like SDL_SCANCODE_W
(when (key-pressed? 'escape) (quit!)) (when (key-pressed? 'w) (move-forward!))
procedure
This is useful for checking multiple keys efficiently in a game loop.
procedure
(reset-keyboard!) → void?
15.2 Modifier Keys
procedure
procedure
(mod-state-has? state mod) → boolean?
state : exact-nonnegative-integer? mod : symbol?
'shift —
Either Shift key 'ctrl —
Either Control key 'alt —
Either Alt key 'gui —
Either GUI/Command/Windows key 'left-shift, 'right-shift —
Specific Shift keys 'left-ctrl, 'right-ctrl —
Specific Control keys 'left-alt, 'right-alt —
Specific Alt keys 'caps-lock, 'num-lock, 'scroll-lock —
Lock keys
(define mods (get-mod-state)) (when (mod-state-has? mods 'ctrl) (printf "Control is held~n"))
15.3 Keyboard Enumeration
procedure
procedure
procedure
procedure
(get-keyboard-name-for-id id) → (or/c string? #f)
id : exact-nonnegative-integer?
procedure
(get-keyboard-focus) → (or/c window? #f)
15.4 Text Input
Text input allows receiving Unicode text from the user, including IME (Input Method Editor) support for non-Latin scripts.
procedure
(start-text-input! win) → void?
win : window?
Once enabled, text-input-event will be generated when the user
types text. This is separate from key events—
(start-text-input! win) ;; Now text-input-event will fire when user types
procedure
(stop-text-input! win) → void?
win : window?
Call this when you no longer need text input (e.g., when leaving a text field) to allow the IME to close.
15.5 Key Symbol Conversion
These functions convert between Racket symbols and SDL scancodes/keycodes.
procedure
(symbol->scancode sym) → (or/c exact-nonnegative-integer? #f)
sym : symbol?
Scancodes represent physical key positions on the keyboard, independent of the current keyboard layout.
procedure
(scancode->symbol scancode) → (or/c symbol? #f)
scancode : exact-nonnegative-integer?
procedure
(symbol->keycode sym) → (or/c exact-nonnegative-integer? #f)
sym : symbol?
Keycodes represent the logical key based on the current keyboard layout. For example, on a QWERTY layout the ’Q’ position produces keycode for ’q’, but on an AZERTY layout it produces keycode for ’a’.
procedure
(keycode->symbol keycode) → (or/c symbol? #f)
keycode : exact-nonnegative-integer?
15.6 Low-Level Key Utilities
These functions provide direct access to SDL’s scancode and keycode APIs.
procedure
(scancode-name scancode) → string?
scancode : exact-nonnegative-integer?
procedure
name : string?
procedure
(key-from-name name) → exact-nonnegative-integer?
name : string?
procedure
(key-from-scancode scancode) → exact-nonnegative-integer?
scancode : exact-nonnegative-integer?
procedure
(scancode-from-key keycode) → exact-nonnegative-integer?
keycode : exact-nonnegative-integer?