Skip to main content

Class: KeyboardInput

Defined in: src/interaction/KeyboardInput.ts:125

Tracks keyboard state for interactive apps and games. Mirrors the Clickable / OrbitControls lifecycle: construct, then enable / disable / dispose.

Constructors​

Constructor​

new KeyboardInput(options, domTarget?): KeyboardInput

Defined in: src/interaction/KeyboardInput.ts:146

Create a KeyboardInput.

Parameters​

options​

KeyboardInputOptions = {}

Behavior options.

domTarget?​

EventTarget

Event target for key events. Defaults to window (or document), or null when no DOM is present.

Returns​

KeyboardInput

Accessors​

hasTarget​

Get Signature​

get hasTarget(): boolean

Defined in: src/interaction/KeyboardInput.ts:166

Whether a DOM target is available. false in a headless environment.

Returns​

boolean


isEnabled​

Get Signature​

get isEnabled(): boolean

Defined in: src/interaction/KeyboardInput.ts:161

Whether the listeners are attached.

Returns​

boolean

Methods​

anyKeyDown()​

anyKeyDown(): boolean

Defined in: src/interaction/KeyboardInput.ts:219

Whether any key is held right now.

Returns​

boolean


bindBool()​

bindBool(machine, inputName, key): Unsubscribe

Defined in: src/interaction/KeyboardInput.ts:292

Hold a StateMachine bool input to match a key: true while held, false on release.

Parameters​

machine​

StateMachineInputSink

The StateMachine (or any StateMachineInputSink).

inputName​

string

The bool input name.

key​

string

The key to track.

Returns​

Unsubscribe

An unsubscribe function.


bindTrigger()​

bindTrigger(machine, triggerName, key): Unsubscribe

Defined in: src/interaction/KeyboardInput.ts:308

Fire a StateMachine trigger input on each key-down.

Parameters​

machine​

StateMachineInputSink

The StateMachine (or any StateMachineInputSink).

triggerName​

string

The trigger input name.

key​

string

The key to track.

Returns​

Unsubscribe

An unsubscribe function.


bindValue()​

bindValue(tracker, key, options): Unsubscribe

Defined in: src/interaction/KeyboardInput.ts:320

Drive a ValueTracker from a key: heldValue while held, releasedValue on release.

Parameters​

tracker​

ValueSink

The ValueTracker (or any ValueSink).

key​

string

The key to track.

options​

BindValueOptions

The held and released values.

Returns​

Unsubscribe

An unsubscribe function.


clear()​

clear(): void

Defined in: src/interaction/KeyboardInput.ts:204

Clear held keys and both per-frame edge sets.

Returns​

void


disable()​

disable(): void

Defined in: src/interaction/KeyboardInput.ts:184

Detach the key listeners and clear all held and edge state, so no key stays stuck. Reads return defaults until enable is called again.

Returns​

void


dispose()​

dispose(): void

Defined in: src/interaction/KeyboardInput.ts:197

Detach listeners and drop every registered callback. Use when the controller is no longer needed.

Returns​

void


enable()​

enable(): void

Defined in: src/interaction/KeyboardInput.ts:173

Attach the key listeners. A no-op when already enabled or headless.

Returns​

void


endFrame()​

endFrame(): void

Defined in: src/interaction/KeyboardInput.ts:340

Alias of update that clears the per-frame edge sets.

Returns​

void


getAxis()​

getAxis(): [number, number]

Defined in: src/interaction/KeyboardInput.ts:246

The raw movement axes from the arrow keys and WASD, each component in {-1, 0, 1}. Up is +y and right is +x; opposite keys cancel.

Returns​

[number, number]

[x, y].


getDirectionVector()​

getDirectionVector(): [number, number]

Defined in: src/interaction/KeyboardInput.ts:257

The movement vector from getAxis, normalized to unit length so a diagonal is not faster than a straight line. A zero axis stays [0, 0].

Returns​

[number, number]

[x, y].


isKeyDown()​

isKeyDown(key): boolean

Defined in: src/interaction/KeyboardInput.ts:214

Whether the key is held right now.

Parameters​

key​

string

A key name in any common spelling ('ArrowUp', 'w', ' ').

Returns​

boolean


onKeyDown()​

onKeyDown(key, callback): Unsubscribe

Defined in: src/interaction/KeyboardInput.ts:270

Register a key-down callback. Repeated auto-repeat events do not re-fire it.

Parameters​

key​

string

A key name, or 'any' for every key.

callback​

KeyCallback

Called with the normalized key and the event.

Returns​

Unsubscribe

An unsubscribe function.


onKeyUp()​

onKeyUp(key, callback): Unsubscribe

Defined in: src/interaction/KeyboardInput.ts:280

Register a key-up callback.

Parameters​

key​

string

A key name, or 'any' for every key.

callback​

KeyCallback

Called with the normalized key and the event.

Returns​

Unsubscribe

An unsubscribe function.


update()​

update(_dt): void

Defined in: src/interaction/KeyboardInput.ts:335

Clear the per-frame pressed and released edge sets. Call once per frame after reading wasPressed / wasReleased.

Parameters​

_dt​

number = 0

Delta time (seconds), accepted for render-loop compatibility.

Returns​

void


wasPressed()​

wasPressed(key): boolean

Defined in: src/interaction/KeyboardInput.ts:228

Whether the key went down during the current frame. Cleared by update / endFrame. Key auto-repeat does not re-fire it.

Parameters​

key​

string

A key name in any common spelling.

Returns​

boolean


wasReleased()​

wasReleased(key): boolean

Defined in: src/interaction/KeyboardInput.ts:237

Whether the key came up during the current frame. Cleared by update / endFrame.

Parameters​

key​

string

A key name in any common spelling.

Returns​

boolean