# core/inputmanager (vanilla.js 0.4.0)

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

Input manager.

## TouchPoint extends Object

One finger. (identifier is the browser's Touch.identifier, positions are canvas-based and view-based)

Example: Read touching and lifted fingers

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

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		const engine = this.getEngine();
		const inputManager = engine.getInputManager();
		const touchPoints = inputManager.getTouchPoints();
		for (const touchPoint of touchPoints) {
			if (touchPoint.phase === TouchPhase.began) {
				console.log(touchPoint.identifier, touchPoint.viewPosition.x, touchPoint.viewPosition.y);
			}
		}
		const endedTouchPoints = inputManager.getEndedTouchPoints();
		for (const touchPoint of endedTouchPoints) {
			console.log(touchPoint.identifier, touchPoint.phase);
		}
	}
}

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

- `identifier` (field, since 0.4.0): Finger number. (Stays the same while touching)
  - returns number
- `phase` (field, since 0.4.0): A TouchPhase value.
  - returns string
- `canvasNativePosition` (field, since 0.4.0): CSS pixel position relative to the canvas top-left.
  - returns Vector2
- `viewPosition` (field, since 0.4.0): View (reference resolution) position.
  - returns Vector2
- `constructor(identifier, phase, canvasNativePosition, viewPosition)` (constructor, since 0.4.0): Constructor.
  - `identifier` number: Finger number. (Touch.identifier)
  - `phase` string: A TouchPhase value.
  - `canvasNativePosition` Vector2: Position from the canvas top-left. (CSS pixels)
  - `viewPosition` Vector2: Position in view coordinates.

## InputManager extends Object

Input manager.

Example: Read keys and mouse buttons

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

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		const engine = this.getEngine();
		const inputManager = engine.getInputManager();
		if (inputManager.isKeyJustPressed("Space")) {
			console.log("jump");
		}
		if (inputManager.isKeyPressed("ArrowRight")) {
			console.log("move right");
		}
		if (inputManager.isMouseButtonJustPressed(MouseButton.right)) {
			const viewInputPosition = inputManager.getViewInputPosition();
			console.log(viewInputPosition.x, viewInputPosition.y);
		}
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const inputManager = engine.getInputManager();
inputManager.setPreventedKeys(["Space", "Tab"]);
inputManager.setTouchFromLeftButtonOnly(true);
engine.run(new MainScene());
```

- `constructor(engine)` (constructor, since 0.0.8): Constructor.
  - `engine` Engine: The engine that owns this manager. (passed to the gamepad manager and used to check text inputs)
- `tick(timeDelta)` (method, since 0.0.8): Update. (Gamepad polling, sets the last device to gamepad if a pad was used, updates input actions)
  - `timeDelta` number: Time since the last frame. (Seconds)
- `clear()` (method, since 0.2.0): Resets all input states to the initial state. (To prevent stuck touch/key when the window loses focus and when the page process is restored)
- `addWheelDelta(deltaX, deltaY)` (method, since 0.2.1): Mouse wheel accumulated value.
  - Accumulates the values coming in through addWheelDelta for one frame, and at frame end
    the engine clears them with clearWheelDelta().
  - `deltaX` number: Horizontal wheel amount to add. (WheelEvent.deltaX)
  - `deltaY` number: Vertical wheel amount to add. (WheelEvent.deltaY, positive when scrolling down)
- `getWheelDeltaX()` (method, since 0.2.1): Returns the horizontal wheel amount accumulated this frame.
  - returns number: Horizontal wheel amount. (sum of WheelEvent.deltaX)
- `getWheelDeltaY()` (method, since 0.2.1): Returns the vertical wheel amount accumulated this frame.
  - returns number: Vertical wheel amount. (sum of WheelEvent.deltaY, positive when scrolling down)
- `hasWheelDelta()` (method, since 0.2.1): Whether any wheel amount accumulated this frame.
  - returns boolean: True if the horizontal or vertical amount is not 0.
- `clearWheelDelta()` (method, since 0.2.1): Clears the accumulated wheel amount. (the engine calls this at the end of the frame)
- `setWheelWithCtrl(isWheelWithCtrl)` (method, since 0.4.0): Sets whether the last wheel was rolled with Ctrl. (The engine calls it from wheel, laptop touchpad pinches also arrive as Ctrl wheel)
  - `isWheelWithCtrl` boolean: Whether it was scrolled with Ctrl.
- `isWheelWithCtrl()` (method, since 0.4.0): Returns whether the last wheel was rolled with Ctrl.
  - returns boolean: True if it was scrolled with Ctrl.
- `pushKey(key)` (method, since 0.0.8): Sets whether a key is pressed.
  - `key` string: Pressed key code. (KeyboardEvent.code)
- `popKey(key)` (method, since 0.0.8): Sets whether a key is released.
  - `key` string: Released key code. (KeyboardEvent.code)
- `isKeyPressed(key)` (method, since 0.0.8): Returns whether a key is pressed.
  - `key` string: Key code. (KeyboardEvent.code, e.g. "KeyW", "Space")
  - returns boolean: True if held down.
- `isKeyJustPressed(key)` (method, since 0.3.0): Whether the key was just pressed this frame.
  - Replaces the wasPressed variable each game used to carry around by hand.
  - Looks at key events since the end of the last frame. A key pressed and released between frames is also true. (In that case isKeyPressed is false and isKeyJustReleased is also true)
  - `key` string: Key code. (KeyboardEvent.code, e.g. "KeyW", "Space")
  - returns boolean: True if pressed this frame.
- `isKeyJustReleased(key)` (method, since 0.3.0): Whether the key was just released this frame.
  - `key` string: Key code. (KeyboardEvent.code, e.g. "KeyW", "Space")
  - returns boolean: True if released this frame.
- `updateFrameSnapshot()` (method, since 0.3.0): Updates the frame snapshot.
  - Called by the engine at frame end. Clears this frame's key and mouse button presses and releases, removes lifted fingers and sets the remaining fingers to stationary.
- `setTouchPressed(value)` (method, since 0.0.8): Sets whether pressed.
  - `value` boolean: Whether pressed this frame.
- `setTouchReleased(value)` (method, since 0.0.8): Sets whether released.
  - `value` boolean: Whether released this frame.
- `setTouchCancelled(value)` (method, since 0.2.0): Sets whether cancelled.
  - `value` boolean: Whether cancelled this frame.
- `setTouchMoved(value)` (method, since 0.0.8): Sets whether being held down.
  - `value` boolean: Whether it is being held.
- `isTouchPressed()` (method, since 0.0.8): Returns whether pressed.
  - returns boolean: True if pressed this frame.
- `isTouchReleased()` (method, since 0.0.8): Returns whether released.
  - returns boolean: True if released this frame.
- `isTouchCancelled()` (method, since 0.2.0): Returns whether cancelled.
  - returns boolean: True if cancelled this frame.
- `isTouchMoved()` (method, since 0.0.8): Returns whether being held down.
  - returns boolean: True if it is being held.
- `setTouchDown(value)` (method, since 0.4.0): Sets whether the touch is held down. (The engine calls it on press and release)
  - `value` boolean: Whether it is held down.
- `isTouchDown()` (method, since 0.4.0): Returns whether the touch is held down. (True from the press frame until release, true even when held without moving)
  - returns boolean: True if it is held down.
- `setCanvasNativeInputPosition(position)` (method, since 0.0.8): Updates the canvasNativeSize based input position.
  - `position` Vector2: Position from the canvas top-left. (CSS pixels, the given vector is rounded in place and stored as is)
- `getCanvasNativeInputPosition()` (method, since 0.0.8): Returns the canvasNativeSize based input position.
  - returns Vector2: Input position from the canvas top-left. (CSS pixels, integers)
- `setViewInputPosition(position)` (method, since 0.0.8): Updates the view's input position.
  - `position` Vector2: Position in view coordinates. (the given vector is rounded in place and stored as is)
- `getViewInputPosition()` (method, since 0.0.8): Returns the view's input position.
  - returns Vector2: Input position in view coordinates. (integers)
- `updateAllGamepads()` (method, since 0.0.8): Updates all gamepads.
- `connectGamepad(gamepad)` (method, since 0.0.9): Gamepad connected.
  - `gamepad` Gamepad: The connected gamepad. (Gamepad of the gamepadconnected event)
- `disconnectGamepad(gamepad)` (method, since 0.0.9): Gamepad disconnected.
  - `gamepad` Gamepad: The disconnected gamepad. (Gamepad of the gamepaddisconnected event)
- `getGamepadManager()` (method, since 0.0.9): Returns the gamepad manager.
  - returns GamepadManager: The gamepad manager.
- `setMouseButtonDown(mouseButton, isDown)` (method, since 0.4.0): Sets the mouse button pressed state. (The engine calls it from mousedown, mouseup)
  - `mouseButton` number: A MouseButton value.
  - `isDown` boolean: True if pressed.
- `isMouseButtonDown(mouseButton)` (method, since 0.4.0): Whether the mouse button is pressed.
  - `mouseButton` number: A MouseButton value.
  - returns boolean: True if held down.
- `isMouseButtonJustPressed(mouseButton)` (method, since 0.4.0): Whether the mouse button was pressed this frame. (Pressed and released between frames is also true)
  - `mouseButton` number: A MouseButton value.
  - returns boolean: True if pressed this frame.
- `isMouseButtonJustReleased(mouseButton)` (method, since 0.4.0): Whether the mouse button was released this frame.
  - `mouseButton` number: A MouseButton value.
  - returns boolean: True if released this frame.
- `setTouchFromLeftButtonOnly(isLeftButtonOnly)` (method, since 0.4.0): Sets whether only the left mouse button is passed as touch. (When off (default), any button is touch as before)
  - If a game that uses right or middle drag for the camera turns it on, the UI is not pressed by those buttons.
  - `isLeftButtonOnly` boolean: If true, only the left button is passed as touch.
- `isTouchFromLeftButtonOnly()` (method, since 0.4.0): Returns whether only the left mouse button is passed as touch.
  - returns boolean: True if only the left button is passed as touch.
- `setContextMenuPrevented(isPrevented)` (method, since 0.4.0): Sets whether to block the canvas's right-click menu. (true initially, same as before)
  - `isPrevented` boolean: If true, the right-click menu is blocked.
- `isContextMenuPrevented()` (method, since 0.4.0): Returns whether the canvas's right-click menu is blocked.
  - returns boolean: True if the right-click menu is blocked.
- `beginTouchPoint(identifier, canvasNativePosition, viewPosition)` (method, since 0.4.0): A finger touched. (The engine calls it for each changed finger of touchstart, an existing number is treated as a move)
  - `identifier` number: Finger number. (Touch.identifier)
  - `canvasNativePosition` Vector2: Position from the canvas top-left. (CSS pixels)
  - `viewPosition` Vector2: Position in view coordinates.
- `moveTouchPoint(identifier, canvasNativePosition, viewPosition)` (method, since 0.4.0): A finger moved. (A finger that touched this frame stays began)
  - `identifier` number: Finger number. (Touch.identifier)
  - `canvasNativePosition` Vector2: Position from the canvas top-left. (CSS pixels)
  - `viewPosition` Vector2: Position in view coordinates.
- `endTouchPoint(identifier, canvasNativePosition, viewPosition, isCancelled)` (method, since 0.4.0): A finger lifted. (Removed from the touching list and put in this frame's lifted list)
  - `identifier` number: Finger number. (Touch.identifier)
  - `canvasNativePosition` Vector2: Position from the canvas top-left. (CSS pixels)
  - `viewPosition` Vector2: Position in view coordinates.
  - `isCancelled` boolean: Cancelled if true, lifted if false.
- `findTouchPoint(identifier)` (method, since 0.4.0): Finds a finger. (null if none)
  - `identifier` number: Finger number. (Touch.identifier)
  - returns TouchPoint | null: The found finger. (null if none)
- `getTouchPoints()` (method, since 0.4.0): List of touching fingers. (In touch order, phases are began, moved, stationary, do not modify the received array)
  - returns TouchPoint[]: List of touching fingers. (the internal array as is)
- `getEndedTouchPoints()` (method, since 0.4.0): List of fingers lifted this frame. (Phases are ended, cancelled, position is where it was lifted)
  - returns TouchPoint[]: List of fingers lifted this frame.
- `setLastInputDevice(inputDevice)` (method, since 0.4.0): Sets the last used input device. (The engine calls it on key, mouse, finger and pad input, also when a game uses an external input source)
  - `inputDevice` string: An InputDevice value.
- `getLastInputDevice()` (method, since 0.4.0): Returns the last used input device. (Initially touch on finger devices, otherwise keyboard)
  - returns string: An InputDevice value.
- `isTouchInput()` (method, since 0.4.0): Whether the last input was a finger. (false for mouse, keyboard, gamepad)
  - returns boolean: True if the last input was a finger.
- `setLastGamepadKind(gamepadKind)` (method, since 0.4.0): Sets the kind of the last used gamepad.
  - `gamepadKind` string: A GamepadKind value.
- `getLastGamepadKind()` (method, since 0.4.0): Returns the kind of the last used gamepad. (Determined by the name of the pad that pressed a button or tilted a stick far, initially other)
  - returns string: A GamepadKind value.
- `setPreventedKeys(keys)` (method, since 0.4.0): Sets the keys whose browser default behavior is blocked. (KeyboardEvent.code list, such as Tab's focus move or Space's page scroll)
  - Not blocked while a text input field has focus. (isTypingInTextField)
  - `keys` string[]: List of key codes to block. (KeyboardEvent.code)
- `getPreventedKeys()` (method, since 0.4.0): Returns the list of keys whose browser default behavior is blocked. (copy)
  - returns string[]: A copy of the blocked key code list.
- `isKeyPrevented(key)` (method, since 0.4.0): Whether the key's default behavior is blocked.
  - `key` string: Key code. (KeyboardEvent.code, e.g. "KeyW", "Space")
  - returns boolean: True if the default action is blocked.
- `isTypingInTextField()` (method, since 0.4.0): Whether typing in a text input field. (Focus is on a text input, textarea, contenteditable, select)
  - returns boolean: True if typing in a text input.
- `getInputActions()` (method, since 0.4.0): Returns the input actions. (Reads keys, pad buttons and axes bound to action names, updated every tick)
  - returns InputActions: Input actions.

## KeyCode (enum)

Keyboard key identifiers. (the KeyboardEvent.code values)

- `a` = `KeyA`: Key A.
- `b` = `KeyB`: Key B.
- `c` = `KeyC`: Key C.
- `d` = `KeyD`: Key D.
- `e` = `KeyE`: Key E.
- `f` = `KeyF`: Key F.
- `g` = `KeyG`: Key G.
- `h` = `KeyH`: Key H.
- `i` = `KeyI`: Key I.
- `j` = `KeyJ`: Key J.
- `k` = `KeyK`: Key K.
- `l` = `KeyL`: Key L.
- `m` = `KeyM`: Key M.
- `n` = `KeyN`: Key N.
- `o` = `KeyO`: Key O.
- `p` = `KeyP`: Key P.
- `q` = `KeyQ`: Key Q.
- `r` = `KeyR`: Key R.
- `s` = `KeyS`: Key S.
- `t` = `KeyT`: Key T.
- `u` = `KeyU`: Key U.
- `v` = `KeyV`: Key V.
- `w` = `KeyW`: Key W.
- `x` = `KeyX`: Key X.
- `y` = `KeyY`: Key Y.
- `z` = `KeyZ`: Key Z.
- `0` = `Digit0`: Digit 0 key. (top row)
- `1` = `Digit1`: Digit 1 key. (top row)
- `2` = `Digit2`: Digit 2 key. (top row)
- `3` = `Digit3`: Digit 3 key. (top row)
- `4` = `Digit4`: Digit 4 key. (top row)
- `5` = `Digit5`: Digit 5 key. (top row)
- `6` = `Digit6`: Digit 6 key. (top row)
- `7` = `Digit7`: Digit 7 key. (top row)
- `8` = `Digit8`: Digit 8 key. (top row)
- `9` = `Digit9`: Digit 9 key. (top row)
- `backspace` = `Backspace`: Backspace key.
- `tab` = `Tab`: Tab key.
- `enter` = `Enter`: Enter key.
- `shift` = `ShiftLeft`: Left Shift key.
- `ctrl` = `ControlLeft`: Left Control key.
- `alt` = `AltLeft`: Left Alt key.
- `capslock` = `CapsLock`: Caps Lock key.
- `escape` = `Escape`: Escape key.
- `space` = `Space`: Space key.
- `pageup` = `PageUp`: Page Up key.
- `pagedown` = `PageDown`: Page Down key.
- `end` = `End`: End key.
- `home` = `Home`: Home key.
- `arrowleft` = `ArrowLeft`: Left arrow key.
- `arrowup` = `ArrowUp`: Up arrow key.
- `arrowright` = `ArrowRight`: Right arrow key.
- `arrowdown` = `ArrowDown`: Down arrow key.
- `insert` = `Insert`: Insert key.
- `delete` = `Delete`: Delete key.
- `f1` = `F1`: F1 key.
- `f2` = `F2`: F2 key.
- `f3` = `F3`: F3 key.
- `f4` = `F4`: F4 key.
- `f5` = `F5`: F5 key.
- `f6` = `F6`: F6 key.
- `f7` = `F7`: F7 key.
- `f8` = `F8`: F8 key.
- `f9` = `F9`: F9 key.
- `f10` = `F10`: F10 key.
- `f11` = `F11`: F11 key.
- `f12` = `F12`: F12 key.
- `scrolllock` = `ScrollLock`: Scroll Lock key.
- `semicolon` = `Semicolon`: Semicolon key.
- `equal` = `Equal`: Equal key.
- `comma` = `Comma`: Comma key.
- `minus` = `Minus`: Minus key.
- `period` = `Period`: Period key.
- `slash` = `Slash`: Slash key.
- `backquote` = `Backquote`: Backquote key.
- `bracketleft` = `BracketLeft`: Left bracket key.
- `backslash` = `Backslash`: Backslash key.
- `bracketright` = `BracketRight`: Right bracket key.
- `quote` = `Quote`: Quote key.
- `meta` = `MetaLeft`: Left Meta key. (Windows key, Command key on Mac)
- `command` = `MetaLeft`: Left Command key. (same value as meta)
- `pause` = `Pause`: Pause key.
- `audiovolumeup` = `AudioVolumeUp`: Volume up key.
- `audiovolumedown` = `AudioVolumeDown`: Volume down key.
- `audiomute` = `AudioVolumeMute`: Mute. (name from the W3C code table)
- `audioplay` = `MediaPlayPause`: Play/pause. (name from the W3C code table, the table has no MediaPlay)
- `audiostop` = `MediaStop`: Media stop key.
- `audioprev` = `MediaTrackPrevious`: Previous track key.
- `audionext` = `MediaTrackNext`: Next track key.
- `numLock` = `NumLock`: Num Lock key.
- `numpadDivide` = `NumpadDivide`: Numpad divide.
- `numpadMultiply` = `NumpadMultiply`: Numpad multiply.
- `numpadSubtract` = `NumpadSubtract`: Numpad subtract.
- `numpadDecimal` = `NumpadDecimal`: Numpad decimal point.
- `numpadAdd` = `NumpadAdd`: Numpad add.
- `numpadEnter` = `NumpadEnter`: Numpad enter.
- `numpad0` = `Numpad0`: Numpad 0.
- `numpad1` = `Numpad1`: Numpad 1.
- `numpad2` = `Numpad2`: Numpad 2.
- `numpad3` = `Numpad3`: Numpad 3.
- `numpad4` = `Numpad4`: Numpad 4.
- `numpad5` = `Numpad5`: Numpad 5.
- `numpad6` = `Numpad6`: Numpad 6.
- `numpad7` = `Numpad7`: Numpad 7.
- `numpad8` = `Numpad8`: Numpad 8.
- `numpad9` = `Numpad9`: Numpad 9.

## MouseButton (enum)

Mouse button. (MouseEvent.button value)

- `left` = `0`: Left button.
- `middle` = `1`: Middle button. (wheel press)
- `right` = `2`: Right button.

## TouchPhase (enum)

Finger phase. (began touched this frame, moved moving, stationary still, ended lifted, cancelled cancelled)

- `began` = `began`: Touched this frame.
- `moved` = `moved`: Moved.
- `stationary` = `stationary`: Stationary.
- `ended` = `ended`: Lifted.
- `cancelled` = `cancelled`: Cancelled. (the browser withdrew the touch)

## InputDevice (enum)

Last used input device.

- `keyboard` = `keyboard`: Keyboard.
- `mouse` = `mouse`: Mouse.
- `touch` = `touch`: Finger.
- `gamepad` = `gamepad`: Gamepad.
