# Input

Bind keys, gamepads, the mouse and touch to your game once, and your scripts never need
to know which one the player is using. A script reads actions, never a device. The project file names the actions and says
which keys, pad buttons, sticks, pointer and fingers drive them; a recording, a test and
a protocol client drive the same actions, so the game cannot tell them from a hand. That
is what lets a run [replay](../concepts/runs.md). Every key name and option is in the
[input reference](../reference/input.md).

## The sample

The controls sample is one square to walk and jump,
a ring that follows the pointer, and a hint that names the jump key:

```toml
# example: controls/game.toml
# The input guide's game: one set of actions driven by keys, a gamepad, the pointer and
# fingers on a touch screen, and a hint that names the keys of the device last used.
# The script reads actions only, so the recording below drives it the same way.
#
#   frost dev game.toml
#   frost test game.toml
scene = "scene.toml"
scripts = ["controls.luau"]
seed = 1
replay = { steps = 150, checksum = "d734d0ac6e707474", engine = "0.0.1 r280" }
# The run's checksum on the way there, written by `frost test --write`: a test that
# fails says between which two of these steps the game first went another way.
checkpoints = []

[window]
size = [320, 180]
background = 0x1b2030

[input]
actions = ["move_x", "move_y", "touching", "jump", "aim_x", "aim_y"]
# Where the mouse, or the first finger, is: each -1 to 1 across the frame.
pointer = { x = "aim_x", y = "aim_y" }
# On a touch screen: a stick on the left half (a stick slot has both axes and a press;
# this game reads only move_x) and a jump button on the right half.
fingers = [
    { region = { x = -1, y = -1, w = 1, h = 2 }, x = "move_x", y = "move_y", press = "touching", stick = 0.15, shown = true },
    { region = { x = 0, y = -1, w = 1, h = 2 }, press = "jump", shown = true, label = "JUMP" },
]
# What `frost test` plays: rest, aim at the top right, walk right, jump, walk left.
recording = [
    { steps = 20, aim_x = 0.8, aim_y = 0.6 },
    { steps = 60, move_x = 1, aim_x = 0.8, aim_y = 0.6 },
    { steps = 1, move_x = 1, jump = 1 },
    { steps = 69, move_x = -0.5 },
]

[input.keys]
move_x = [{ negative = "A", positive = "D" }, { negative = "Left", positive = "Right" }, "PadLeftX"]
jump = ["Space", "PadA"]
```

```lua
-- example: controls/controls.luau
-- Reads actions, never a device: a key, a stick, a finger and a recording are the same.
local SPEED, JUMP, GRAVITY, FLOOR = 90, 220, 600, 32

state.vy = state.vy or 0

function step(dt)
    local hero = find("hero")
    local x = get(hero, "Transform", "x") + input.get(0, "move_x") * SPEED * dt
    local y = get(hero, "Transform", "y")
    if y <= FLOOR and input.held(0, "jump") then
        state.vy = JUMP
    end
    state.vy -= GRAVITY * dt
    y = math.max(FLOOR, y + state.vy * dt)
    if y == FLOOR then
        state.vy = 0
    end
    set(hero, "Transform", "x", math.clamp(x, 8, 312))
    set(hero, "Transform", "y", y)

    -- The pointer's place in frame units: (value + 1) / 2 times the frame's side.
    local width, height = frame.size()
    local aim = find("aim")
    set(aim, "Transform", "x", (input.get(0, "aim_x") + 1) / 2 * width)
    set(aim, "Transform", "y", (input.get(0, "aim_y") + 1) / 2 * height)

    -- The names of the keys that jump on the device used last: SPACE, or the pad's A.
    local keys = input.keys("jump")
    set(find("hint"), "Text", "text", "JUMP: " .. string.upper(keys[1] or "?"))
end
```

## Actions and keys

`[input] actions` names what a script reads. `[input.keys]` binds each one:

- A key name alone is a button for player 0: 1 while it is down. `input.held(0, "jump")`
  says whether it is held; `input.get(0, "jump")` reads its value.
- A table with `negative` and `positive` is an axis: -1 to 1. `input.get(0, "move_x")`.
- A list binds several keys, pads or players to one action; an axis is the sum of its
  bindings, held to -1 to 1.
- `player = 1` in a binding gives it to the second player, so two people can share one
  keyboard (`[input] players` says how many play).

Keys are named by what a US keyboard prints on them: `"A"` to `"Z"`, `"0"` to `"9"`,
`"Space"`, `"Enter"`, `"Tab"`, `"Escape"`, `"Left"`, `"Right"`, `"Up"`, `"Down"`, and the
pointer's buttons `"MouseLeft"`, `"MouseRight"`, `"MouseMiddle"`. There are no names for
Shift, Ctrl or Alt yet.

## Gamepads

A pad is bound in the same table: its buttons are keys (`"PadA"` at the bottom, `"PadB"`,
`"PadX"`, `"PadY"`, the shoulders, the d-pad `"PadUp"` to `"PadRight"`, `"PadStart"`,
`"PadSelect"`, the sticks pressed), and a stick is an axis by its name alone
(`"PadLeftX"`, `"PadLeftY"`, `"PadRightX"`, `"PadRightY"`, right and up as 1), with a dead
centre. A player's pad is the one with the player's number, and keeps it while it is
plugged in. An exported player reads pads only when its game binds one, so a game with
none ships no pad code.

## The pointer

The mouse, or a finger on a touch screen, is two actions the project names with
`pointer = { x = "aim_x", y = "aim_y" }`, each -1 at the frame's left or bottom edge and 1
at its right or top. In frame units that is `(x + 1) / 2 * width`, as the sample does with
`frame.size()`. Its buttons are keys; a finger is `"MouseLeft"`. There is no mouse
capture and no mouse look yet.

## Fingers

`[input] fingers` gives a touch screen up to four slots, each a region of the frame (in
the pointer's units, the whole frame 2 by 2) and its actions. A finger that comes down in
a free slot's region drives that slot until it lifts, wherever it goes:

- a slot with `x`, `y`, `press` and `stick` is a thumb stick: `x` and `y` are how far the
  thumb is from where it came down;
- a slot with `press` alone is a button.

`shown = true` draws the control (a disc with its `label`, or a stick's ring and knob)
over the game inside the screen's safe area. It is the player's picture only: no thing,
no checksum, no shot. `while_hidden` and `while_shown` tie a slot to whether an interface
node is shown, as Embers does with its title panel.

## Showing the right key

`input.keys("jump")` lists the names of the keys bound to an action on the device the
player used last: the keyboard's (`Space`) until a pad is used, then the pad's (`PadA`).
The sample writes it into its hint. Which device was last used is recorded, so a replay
shows the same hint.

## Recordings

`[input] recording` is a list of stretches, each holding some actions at values for some
steps; an action left out is at rest. `input.history` over the protocol returns a played
session in exactly this form, and a long one goes in a file of its own (`recording =
"run.toml"`). A recording also says the frame it was made in when that is not `[window]
size`, so a game that fills a phone's screen replays at that shape.

## Checks

```sh
frost check game.toml
frost test game.toml
```

Over the protocol, `input.set {player, action, value}` holds an action and `game.step`
steps on it: how an agent plays a game by hand, with the same result as a person at the
keys.
