keys

Keyboard and mouse.

Key names are SDL's: letters "A"-"Z", digits "0"-"9", "F2"-"F12", "Space", "Return", "Escape", "Tab", "Backspace", "Left Shift", "Right Shift", "Left Ctrl", "Right Ctrl", "Left Alt", "Right Alt", "Up", "Down", "Left", "Right", "Insert", "Delete", "Home", "End", "PageUp", "PageDown", "Keypad 0"-"Keypad 9", "Keypad +", "Keypad -", "-", "=", "[", "]", ";", "'", ",", ".", "/", "\\". F1, F9 and ` belong to the mod menu, reload and console.

Players choose their own keys for the game's actions (Options → [CONTROLS]); by default W A S D move, Shift runs, Space jumps, the mouse fires and holds the binoculars, R / B / M / C / Q / 1-9 do the rest. Bind your mod's keys to something else, and show the player's own key for a game action with keys.binding: the actions are forward back left right run jump fire zoom binoculars weapon slot1-slot9 map call resupply cameraroll trophylock trophydelete.

Functions

# keys.bind(key, fn, options?) → integer

Calls fn when the key goes down. The game does not see the key, unless fn returns false. With touch, a phone shows an on-screen button for the key (see touch.button); the PC only has the key.

ParameterType
keystringkey name
fnfunctionfn(e), the key event
options optionalinteger|tablea priority, or a table: priority, touch (a touch.button options table: on a phone a button that presses this key; since API 3)
Returns
integerlistener id (events.off)
keys.bind("H", function()
  world.god(not world.god())
  game.message("God mode " .. (world.god() and "on" or "off"))
end)

-- the same, with a button on a phone
keys.bind("H", toggleGod, {touch = {label = "GOD"}})

# keys.down(key) → boolean

Whether a key is held right now.

ParameterType
keystringkey name
Returns
booleantrue while held
on("frame", function(e)
  if keys.down("Left Shift") then stamina = stamina - e.dt end
end)

# keys.mouse() → number, number, boolean, boolean, boolean

Mouse position and buttons.

Returns
numberx
numbery
booleanleft button
booleanright
booleanmiddle

# keys.binding(action) → string?, string?

The player's keys for one of the game's actions.

ParameterType
actionstringa game action: "fire", "jump", "map" ... (see above)
Returns
string?the first key / button, e.g. "Mouse Left" or "Space"
string?the second one
game.message("Press " .. (keys.binding("call") or "?") .. " to call")