website is under construction
Lumen

Keyboard

import "lumen:keyboard"

The keyboard module reads the state of the keyboard, and switches text entry on and off.

Detecting Key Presses

isDown reports whether a key is currently held. It is true on every frame the key is down, which is what continuous movement wants.

function update(dt) {
  if (keyboard.isDown('w')) {
    // Move the player up
  }

  if (keyboard.isDown('a')) {
    // Move the player left
  }

  if (keyboard.isDown('s')) {
    // Move the player down
  }

  if (keyboard.isDown('d')) {
    // Move the player right
  }
}

Every method here takes any number of key names and is true if any of them matches, so one action can be bound to several keys in a single call:

if (keyboard.isDown('left', 'a')) {
  // Move the player left
}

Methods

MethodWhat it reports
isDown(key, ...)The key is held down right now.
isUp(key, ...)The key is not held down.
wasPressed(key, ...)The key went down this frame.
wasReleased(key, ...)The key came up this frame.
startTextInput()Begin delivering typed text to the textinput callback.
stopTextInput()Stop.
isTextInputActive()Whether text input is on.

Key Names

Names are SDL key names, matched case-insensitively: 'left', 'right', 'up', 'down', 'space', 'escape', 'return', 'tab', 'backspace', 'lshift', 'f1' through 'f12', 'a' through 'z', '0' through '9', and so on.

An unrecognised name is an error rather than a silently false result, so a typo shows up the first time the line runs instead of as a control that quietly does nothing.

Held vs. Pressed

The distinction matters more than it looks.

  • isDown is true every frame the key is held. Use it for movement, and anything else that should keep happening.
  • wasPressed is true for the single frame the key went down. Use it for menus, jumps, dialogue advance — anything that should happen once per physical press.
function update(dt) {
  if (keyboard.isDown('right')) { game.x = game.x + 180 * dt } // continuous
  if (keyboard.wasPressed('space')) { jump() }                 // once
}

The keypressed(key, isRepeat) callback does the same job as wasPressed from the other direction, and tells you whether the event came from the operating system's key repeat.

Text Input

Reading letters through isDown gives you key positions, not characters — it can't tell you about shift, layouts, or accents. For anything the player types, switch on text input and take the characters from the textinput callback.

function load() {
  game.name = ''
}

function keypressed(key, isRepeat) {
  if (key == 'return') {
    keyboard.stopTextInput()
  }
}

function textinput(text) {
  game.name = game.name + text
}

Call keyboard.startTextInput() when a field takes focus and stopTextInput() when it loses it. isTextInputActive() reports which state you are in — useful for suppressing game controls while a field has focus.

Lumen gives you the raw characters and nothing else. Caret movement, selection, backspace handling, and clipboard paste are yours to build; system.getClipboardText() is there for the last of those.