# core/inputactions (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/inputactions/

Input actions.

## InputActions extends Object

Input actions.
- Binds keys, gamepad buttons, and one side of gamepad axes to action names (strings the game defines, like "confirm", "left"). Several can be bound to one action, and it is pressed if any one of them is pressed.
- The engine's InputManager holds one (getInputActions) and calls update in tick at the start of the frame. The game only reads.
- Like the Unity Input System, it updates once in update at the start of the frame, and reads within that frame (readGamepadStick and readMoveAxis too) return the values recorded then. (same as the Rust engine)
- A key pressed and released between frames counts as pressed once in that frame. (InputManager.isKeyJustPressed)
- For an action with setRepeat, holding it makes isJustPressed true again at each interval after a wait. (menu selection)
- Axes subtract the dead zone (setDeadZone) and rescale the rest to 0 ~ 1. Sticks are filtered by length, bound axes by their one-side value.
- External sources (addSource) are where the game plugs in input the engine doesn't know (such as a platform SDK's pad). They are objects with the functions below.
    readActionValue(action) → a number 0 ~ 1 or a boolean. (pressed if 0.5 or more)
    readMoveAxis() → { x, y } or null. (optional, merged into readMoveAxis)
    readGamepadKind() → a GamepadKind value. (optional, in frames where this source provided input, records the last device as a gamepad of that kind)
- Keys are not listened to while typing in a text field. (turned off with setKeysIgnoredWhileTyping)

Example: Bind keys, buttons and axes to actions and read them

```js
import { Engine, EngineConfiguration, Scene } from "./build/vanilla.js";

class MainScene extends Scene {
	initialize(engine) {
		const inputManager = engine.getInputManager();
		const inputActions = inputManager.getInputActions();
		inputActions.bindKey("confirm", "Enter");
		inputActions.bindKey("confirm", "Space");
		inputActions.bindGamepadButton("confirm", 0);
		inputActions.bindKey("down", "ArrowDown");
		inputActions.bindAxis("down", 1, 1);
		inputActions.setRepeat("down", 0.4, 0.1);
	}

	tick(timeDelta) {
		super.tick(timeDelta);
		const engine = this.getEngine();
		const inputManager = engine.getInputManager();
		const inputActions = inputManager.getInputActions();
		if (inputActions.isJustPressed("confirm")) {
			console.log("confirm");
		}
		if (inputActions.isJustPressed("down")) {
			console.log("next item");
		}
		const moveAxis = inputActions.readMoveAxis();
		if (moveAxis.x !== 0 || moveAxis.y !== 0) {
			console.log(moveAxis.x, moveAxis.y);
		}
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.4.0): Constructor.
- `createActionState(action)` (method, since 0.4.0): Returns the action's state slot. (creates it if missing)
  - `action` string: Action name. (text chosen by the game)
  - returns object: The action's state. { isPressed, wasPressed, value, repeatSeconds, isRepeatFired }
- `bindKey(action, key)` (method, since 0.4.0): Binds a key. (KeyboardEvent.code, a KeyCode value)
  - `action` string: Action name. (text chosen by the game)
  - `key` string: Key code to bind. (KeyboardEvent.code, the same key only once)
- `bindGamepadButton(action, buttonCode)` (method, since 0.4.0): Binds a gamepad button. (GamepadButtonCode, all connected pads)
  - `action` string: Action name. (text chosen by the game)
  - `buttonCode` number: Button index to bind. (W3C standard mapping, 0 is the A or cross button)
- `bindAxis(action, axisIndex, direction)` (method, since 0.4.0): Binds one side of a gamepad axis. (GamepadAnalogStickCode, direction 1 is the + side, -1 is the - side, axis Y is + downward)
  - Tilting that way by setAxisPressThreshold counts as pressed, and the value (readValue) is the tilt minus the dead zone.
  - `action` string: Action name. (text chosen by the game)
  - `axisIndex` number: Axis index. (0 left horizontal, 1 left vertical, 2 right horizontal, 3 right vertical)
  - `direction` number: Side to bind. (+ side if 0 or more, - side if negative)
- `unbindAction(action)` (method, since 0.4.0): Unbinds everything bound to an action. (before rebinding in a key remapping screen, repeat settings remain)
  - `action` string: Action name. (text chosen by the game)
- `getBoundKeys(action)` (method, since 0.4.0): Returns the list of keys bound to an action. (a copy, for drawing button prompts)
  - `action` string: Action name. (text chosen by the game)
  - returns string[]: A copy of the bound key code list. (empty array if none)
- `setRepeat(action, delaySeconds, intervalSeconds)` (method, since 0.4.0): Sets repeat while held. (isJustPressed is true every interval seconds after the wait seconds, an interval of 0 or less turns repeat off)
  - `action` string: Action name. (text chosen by the game)
  - `delaySeconds` number: Time to wait before the first repeat. (seconds, negative becomes 0)
  - `intervalSeconds` number: Repeat interval. (seconds, 0 or less turns repeat off)
- `setDeadZone(deadZone)` (method, since 0.4.0): Sets the dead zone. (0 ~ less than 1, initially 0.2)
  - `deadZone` number: Dead zone. (clamped to 0 ~ 0.99)
- `getDeadZone()` (method, since 0.4.0): Returns the dead zone.
  - returns number: Dead zone. (0 ~ 0.99)
- `setAxisPressThreshold(axisPressThreshold)` (method, since 0.4.0): Sets the tilt at which an axis-bound action counts as pressed. (0 ~ 1, initially 0.5)
  - `axisPressThreshold` number: Tilt counted as pressed. (clamped to 0 ~ 1)
- `getAxisPressThreshold()` (method, since 0.4.0): Returns the tilt at which an axis-bound action counts as pressed.
  - returns number: Tilt counted as pressed. (0 ~ 1)
- `setKeysIgnoredWhileTyping(isIgnored)` (method, since 0.4.0): Sets whether keys are ignored while typing in a text field. (initially true)
  - `isIgnored` boolean: If true, keys are ignored while typing in a text input.
- `isKeysIgnoredWhileTyping()` (method, since 0.4.0): Returns whether keys are ignored while typing in a text field.
  - returns boolean: True if keys are ignored while typing in a text input.
- `addSource(source)` (method, since 0.4.0): Adds an external source. (unchanged if already present)
  - `source` object: { readActionValue(action), readMoveAxis(), readGamepadKind() }

  Example: Plug in an outside input source

  ```js
  import { Engine, EngineConfiguration, GamepadKind } from "./build/vanilla.js";
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  const inputManager = engine.getInputManager();
  const inputActions = inputManager.getInputActions();
  inputActions.bindKey("confirm", "Enter");
  const platformSource = {
  	readActionValue(action) {
  		if (action === "confirm") {
  			return 1;
  		}
  		return 0;
  	},
  	readMoveAxis() {
  		return { x: 0.5, y: 0 };
  	},
  	readGamepadKind() {
  		return GamepadKind.xbox;
  	},
  };
  inputActions.addSource(platformSource);
  inputActions.update(inputManager, 0);
  const moveAxis = inputActions.readMoveAxis();
  const isConfirmPressed = inputActions.isPressed("confirm");
  const lastInputDevice = inputManager.getLastInputDevice();
  console.log(isConfirmPressed, moveAxis.x, lastInputDevice);
  inputActions.removeSource(platformSource);
  ```

- `removeSource(source)` (method, since 0.4.0): Removes an external source.
  - `source` object: A source added before. (does nothing if not found)
- `update(inputManager, timeDelta)` (method, since 0.4.0): Per-frame update. (called by InputManager.tick)
  - `inputManager` object: InputManager
  - `timeDelta` number: Time since the last frame. (Seconds)
- `clear()` (method, since 0.4.0): Resets everything to released. (when the window loses focus, releases are not reported either)
- `isPressed(action)` (method, since 0.4.0): Whether the action is held.
  - `action` string: Action name. (text chosen by the game)
  - returns boolean: True if held down. (false for an unknown action)
- `isJustPressed(action)` (method, since 0.4.0): Whether it was pressed this frame. (also true on repeats for actions with setRepeat)
  - `action` string: Action name. (text chosen by the game)
  - returns boolean: True if pressed this frame or a repeat fired.
- `isJustReleased(action)` (method, since 0.4.0): Whether it was released this frame.
  - `action` string: Action name. (text chosen by the game)
  - returns boolean: True if released this frame.
- `readValue(action)` (method, since 0.4.0): The action's value. (0 ~ 1, 1 for keys and digital buttons, triggers and axes by how far they are tilted)
  - `action` string: Action name. (text chosen by the game)
  - returns number: The action's value. (0 ~ 1, 0 for an unknown action)
- `readAxis(negativeAction, positiveAction)` (method, since 0.4.0): An axis made from two actions. (-1 ~ 1, the difference between the two actions' values)
  - `negativeAction` string: Action name for the negative side. (its value is subtracted)
  - `positiveAction` string: Action name for the positive side. (its value is added)
  - returns number: Axis value of the positive side minus the negative side. (-1 ~ 1)
- `readGamepadStick(xAxisIndex, yAxisIndex)` (method, since 0.4.0): Gamepad stick. (the most tilted among connected pads, dead zone subtracted by length, length 1 or less, right +x down +y)
  - `xAxisIndex` number: GamepadAnalogStickCode
  - `yAxisIndex` number: GamepadAnalogStickCode
  - returns Vector2: Stick vector. (length 0 ~ 1, right +x down +y, (0, 0) before update)
- `readMoveAxis()` (method, since 0.4.0): Movement axis. (the longest of WASD and arrow keys, the left stick, and external sources' movement axes, length 1 or less, right +x down +y)
  - Keys have length 1 diagonally too. Keys are not listened to while typing in a text field. The values are the ones recorded in this frame's update.
  - returns Vector2: Move vector. (length 0 ~ 1, right +x down +y, (0, 0) before update)
- `applyDeadZone(value)` (method, since 0.4.0): Subtracts the dead zone. (0 at or below the dead zone, rescales the rest to 0 ~ 1 so a slight tilt doesn't jump)
  - `value` number: Value to filter. (a length or one-sided tilt of 0 or more)
  - returns number: Value with the dead zone removed. (0 ~ 1)
- `clampLength(x, y)` (method, since 0.4.0): Clamps to length 1.
  - `x` number: X component. (0 if not a finite number)
  - `y` number: Y component. (0 if not a finite number)
  - returns Vector2: A new vector with length 1 or less.
