# input

Part of the [scripting reference](index.md), written by the engine so it matches your `frost`. Start with [your first game](../start/first-game.md) if you are new.

Use when binding keyboard, mouse, touch or gamepad controls, reading player actions or setting up multiplayer input.

| In a script | Does |
| --- | --- |
| `input.get(player, "action")` | A player's value for an action this step. Players count from 0 |
| `input.held(player, "action")` | Whether a player holds a button action |
| `input.keys("action", player?)` | List of unique key/pad control names bound to the action for the last-used device; player defaults to 0. Before input, keyboard/pointer. Empty if unbound on that device; unknown actions are errors. Device changes are recorded and restored. Network play uses keyboard labels on all peers |
| `net.host()`, `net.join(address)`, `net.cancel()`, `net.state()`, `net.player()` | A menu's requests to play over a network, done after the step, and where it stands. Refused once the game plays with others. See docs/multiplayer.md |

### Input

A script reads actions, never a device: `input.get(0, "move_x")`. The project file says
which keys drive which action (`[input.keys]`); a recording, a test or a protocol client
drives the same actions, so the game cannot tell them from a hand.

The pointer (a mouse, or a finger on a touch screen) is actions too. Its buttons are
keys, bound like any key, and a finger is `"MouseLeft"`: `fire = ["Space", "MouseLeft"]`.
Its place goes to the two actions the project names with `pointer` under `[input]`, each
-1 to 1 across the frame. In frame units, the units of an entity with `Screen`:

```lua
local x = (input.get(0, "aim_x") + 1) / 2 * 320 -- the frame's width
local y = (input.get(0, "aim_y") + 1) / 2 * 180 -- its height
```

On a touch screen a game can follow two thumbs at once: each finger slot under
`[input] fingers` is a part of the frame, and a finger that comes down in it drives that
slot's three actions (its place and its press) until it lifts. They are read like any
actions: `input.get(0, "move_x")`, `input.get(0, "move")`.

A gamepad is bound in the same table: a button is a key, and a stick is an axis by its
name alone, with a dead centre. The project file's `[input.keys]` lists every name:

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

```toml
# Optional: the actions that drive the game's menus and controls, each one of the
# `actions` under `[input]`, and each optional. `x` and `y` are axes that move the
# focus (and a slider), `accept` uses the node with the focus, `back` goes back out of
# a menu, and `press` is the pointer's button; the pointer's place is player 0's
# `[input] pointer`. The game may play with the same actions. `[ui.theme]` gives the colours (0xRRGGBB) a usable node takes: `normal`,
# `focus`, `pressed`, `off` and `locked`; one left out is not written. `start` names
# the node a menu opens on, if it is shown then; left out, the highest. Only a game with this table, or with a `Node` in a scene, has the
# interface's components. See Interface.
[ui]
# fit = 720 # Optional design height: screen-space drawing scales with the frame.
x = "move_x"
accept = "jump"
start = "play"
```

```toml
# Optional: the picture. `size` is the frame in pixels, each side from 1 to 4096; one
# world unit is one pixel, the origin is the bottom-left corner, x grows to the right
# and y grows up. `background` is the colour behind everything, as 0xRRGGBB. Left
# out: 960 by 540 and 0x15171c. A window may be any size: the frame keeps its shape, with the background beside it.
# `filter` is how every picture is sampled when a sprite shows it at another size
# than its own: "nearest" keeps pixels sharp, for pixel art; "linear" blends them.
# Left out: "nearest".
# `orientation` is how a phone holds the game: "landscape" or "portrait"; left out,
# landscape when the written frame is wider than tall. `fit` is what the frame does on a
# screen of another shape: "letterbox" (left out) keeps the frame's size and shape, with
# background bars beside it; "fill" takes the screen's shape (on a phone its safe part),
# keeping the height and growing the width, or keeping the width for a tall frame.
# `frame.size()` reports that frame; anchored UI follows its edges and cameras see more.
# A recording says the frame it was made in (`[input] frame`), so it comes to the same
# checksum anywhere.
[window]
size = [960, 540]
background = 0x15171c
filter = "nearest"
orientation = "landscape"
fit = "letterbox"
```

```toml
# Optional: how the game is played over a network by rollback (docs/multiplayer.md;
# the protocol's net.host, net.join and net.run). How many players it has is
# [input] players, 2 to 8 for a network. `delay` is how many steps a player's input
# waits before its step, 0 to 8; `window` how many steps may be played on predicted
# input and so played again, 1 to 16; `sync_every` how many steps apart the peers
# compare the world's checksum, 1 to 600. Left out: as written here.
[net]
delay = 2
window = 8
sync_every = 30
```

```toml
# Optional: the keys that drive the actions when the game runs in a window (`frost dev`,
# the player) or in a browser. A key is named by what a US keyboard prints on it:
# "A" to "Z", "0" to "9", "Space", "Enter", "Tab", "Escape", "Left", "Right", "Up",
# "Down", and the pointer's buttons "MouseLeft", "MouseRight", "MouseMiddle" (a finger
# on a touch screen is "MouseLeft"). A gamepad is written the same way: its buttons
# are "PadA", "PadB", "PadX", "PadY" (A at the bottom), "PadL1", "PadR1", "PadL2",
# "PadR2", "PadSelect", "PadStart", "PadL3", "PadR3" (the sticks pressed), "PadUp",
# "PadDown", "PadLeft", "PadRight", and a stick is an axis by its name alone:
# "PadLeftX", "PadLeftY", "PadRightX", "PadRightY", with right and up as 1. A player's
# pad is the one with the player's number. A key name alone is a button for player 0: 1 while the key is down. A table
# is a button (`key`) or an axis (1 while `positive` is down, -1 while `negative`
# is), for `player` (left out: player 0). A list of them binds the action for several
# players on the one keyboard, or gives one player several keys for it: any of them
# presses a button, and an axis is the sum of its bindings, held to -1 to 1.
#
# Optional, beside `actions`: the two actions that are told where the pointer is (the
# mouse, or a finger): pointer = { x = "aim_x", y = "aim_y" }, for `player` (left out:
# player 0). Each is an axis like any other: x is -1 at the frame's left edge and 1 at its right, y is -1 at the
# bottom and 1 at the top, and a pointer outside the frame is at the nearest edge. In
# frame units that is (x + 1) / 2 * width and (y + 1) / 2 * height.
#
# Optional: finger slots for a touch screen, up to 4, each a `region` and three declared
# actions: fingers = [{ region = { x = -1, y = -1, w = 1, h = 2 }, x = "move_x",
# y = "move_y", press = "move" }] (that region is the left half), for `player` (left out:
# player 0). A region is in the units of the pointer's place: x, y its corner nearest -1,
# -1, then its width and height, the whole frame being 2 by 2; the frame is the game's own,
# as the pointer's is, and a finger over the bars beside it is at its nearest edge. A
# finger that comes down takes the first free slot, in the list's order, whose region holds
# its place (regions may overlap), and keeps it until it lifts, wherever it goes: the slot's
# `x` and `y` hold its place, -1 to 1 like the pointer's, and `press` is 1 while it is down.
# A finger in no free slot's region is no slot's. Slots are actions, so a recording, a test
# and a protocol client play them as they play keys. The first finger is still also the
# pointer with "MouseLeft": a menu under a slot's region sees both, so keep a game's buttons
# out of its slots' regions, or read one of the two.
# With `stick = 0.2` a slot's `x` and `y` are how far the thumb is from where it came
# down, over that fraction of the frame's height, kept within a unit circle; lifted, both
# are 0. A slot with no `x` and `y` is a button: `press` alone.
# `shown = true` draws the slot: a disc with its `label` on it (left out: the name of its
# `press` action), or a stick's ring and the knob that follows the thumb, resting in the
# region's lower outer corner. A shown control is the size of a thumb, in points, inside
# the screen's safe area, translucent at rest and brighter while pressed, and never over
# a UI node that shows. It is part of the player's picture and of nothing else: no
# entity, not in the world, a checksum or a shot. `while_hidden = "panel"` draws a slot
# and gives it touches only while the node of that name is hidden, and `while_shown` only
# while it shows (a title's START, the stick of the play under it).
# `[input] controls = { color = 0xffffff, alpha = 0.35, size = 64 }` styles every slot:
# `size` is a button's width in points, and `alpha = 0` hides them all.
[input.keys]
jump = "Space"
move_x = [
    { negative = "A", positive = "D" },
    { player = 1, negative = "Left", positive = "Right" },
]
```
