On this page:
15.1 Keyboard State
key-pressed?
get-keyboard-state
reset-keyboard!
15.2 Modifier Keys
get-mod-state
mod-state-has?
15.3 Keyboard Enumeration
has-keyboard?
get-keyboards
get-keyboard-count
get-keyboard-name-for-id
get-keyboard-focus
15.4 Text Input
start-text-input!
stop-text-input!
15.5 Key Symbol Conversion
symbol->scancode
scancode->symbol
symbol->keycode
keycode->symbol
15.6 Low-Level Key Utilities
scancode-name
scancode-from-name
key-from-name
key-from-scancode
scancode-from-key
9.0.0.11

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?)
Returns #t if the specified key is currently pressed.

The key can be either:
  • 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!))

Returns a vector of boolean values representing the current state of all keys. Index into the vector using SDL scancode constants.

This is useful for checking multiple keys efficiently in a game loop.

procedure

(reset-keyboard!)  void?

Resets the keyboard state. This generates key-up events for all currently pressed keys, useful when regaining window focus.

15.2 Modifier Keys🔗ℹ

Returns the current modifier key state as a bitmask. Use mod-state-has? to check for specific modifiers.

procedure

(mod-state-has? state mod)  boolean?

  state : exact-nonnegative-integer?
  mod : symbol?
Returns #t if the modifier state includes the specified modifier.

Modifier symbols include:
  • '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

(has-keyboard?)  boolean?

Returns #t if a keyboard is available.

Returns a list of keyboard device IDs.

Returns the number of connected keyboards.

procedure

(get-keyboard-name-for-id id)  (or/c string? #f)

  id : exact-nonnegative-integer?
Returns the name of the keyboard with the given ID, or #f if not found.

procedure

(get-keyboard-focus)  (or/c window? #f)

Returns the window that currently has keyboard focus, or #f if none.

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?
Enables text input events for the specified window.

Once enabled, text-input-event will be generated when the user types text. This is separate from key events—key events give you raw key presses, while text input gives you the resulting characters (including those composed via dead keys or IME).

(start-text-input! win)
;; Now text-input-event will fire when user types

procedure

(stop-text-input! win)  void?

  win : window?
Disables text input events for the specified 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?
Converts a key symbol to its SDL scancode, or #f if not recognized.

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?
Converts an SDL scancode to its key symbol, or #f if not recognized.

procedure

(symbol->keycode sym)  (or/c exact-nonnegative-integer? #f)

  sym : symbol?
Converts a key symbol to its SDL keycode, or #f if not recognized.

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?
Converts an SDL keycode to its key symbol, or #f if not recognized.

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?
Returns the human-readable name for a scancode.

procedure

(scancode-from-name name)  exact-nonnegative-integer?

  name : string?
Returns the scancode for the given key name.

procedure

(key-from-name name)  exact-nonnegative-integer?

  name : string?
Returns the keycode for the given key name.

Converts a scancode to the corresponding keycode based on current layout.

Converts a keycode to the corresponding scancode based on current layout.