
# Input

## `input`

Raw window state, before any keybind mapping.

| Function | Does |
|----------|------|
| `input.key_down(code)` | `true` while a key is held. GLFW code, `32` is space |
| `input.mouse_down(btn)` | `true` while a button is held: `0` left, `1` right, `2` middle |
| `input.mouse_pos()` | Cursor `x, y` in `render_2d` canvas pixels |
| `input.mouse_locked()` | `true` while the cursor is grabbed |
| `input.lock()`, `input.unlock()` | Grabs the cursor (hidden and locked), or releases it |

```lua
if input.key_down(340) then     -- left shift
    local x, y = input.mouse_pos()
    print(x, y)
end
```

For a bind the player can rebind in the panel, declare a [hotkey setting](settings.md#key-binds)
instead.

## `options`

Vanilla keybinds and the camera view.

| Function | Does |
|----------|------|
| `options.key(name)` | `true` while that binding is pressed |
| `options.set_key(name, down)` | Holds or releases the binding |
| `options.perspective()` | `"first_person"`, `"third_person_back"`, `"third_person_front"` |
| `options.set_perspective(p)` | Forces the view; unknown names are ignored |
| `options.sensitivity()` | Mouse sensitivity slider, `0..1`, default `0.5` |
| `options.render_distance()` | Render distance in chunks |
| `options.set_render_distance(chunks)` | Sets it, clamped to the vanilla slider range |
| `options.simulation_distance()` | Simulation distance in chunks |
| `options.set_simulation_distance(chunks)` | Sets it, clamped to the vanilla slider range |
| `options.reload_chunks()` | Rebuilds all chunk sections, like F3+A |

`name` is one of `"forward"`, `"backward"`, `"left"`, `"right"`, `"jump"`, `"sneak"`,
`"sprint"`, `"attack"`, `"use"`.

`set_key` drives the vanilla binding, and a key you hold stays down until a physical key
event releases it, so call it every tick to keep holding.

```lua
module:event("tick", function()
    if options.perspective() == "first_person" then
        options.set_perspective("third_person_back")
    end
    options.set_key("forward", true)
end)
```
