Skip to main content
Defined in: modules/input/src/controller/controller.library.ts:37 Built-in controller library, based on the Gamepad API.

Remarks

Polls navigator.getGamepads() once per frame and exposes a snapshot of every connected controller on Context.controller. Buttons and axes follow the W3C “standard” layout (ControllerButton, ControllerAxis); every method takes an optional controller index, 0 by default. Client-only — __init throws if the Gamepad API is unavailable. Browsers only expose a controller after the user pressed one of its buttons.

Example

Extends

  • Library

Constructors

Constructor

new ControllerLibrary(options?): ControllerLibrary
Defined in: modules/input/src/controller/controller.library.ts:43

Parameters

options?
ControllerLibraryOptions = {}

Returns

ControllerLibrary

Overrides

Library.constructor

Properties

key

readonly key: "controller"
Defined in: modules/input/src/controller/controller.library.ts:38 This library’s identity: its slot on Context, its registry key, and the identifier used to reference it in ordering relationships.

Overrides

Library.key

Accessors

relationships

Get Signature

get relationships(): LibraryRelationships
Defined in: packages/common/dist/index.d.ts:512 Internal
Returns
LibraryRelationships

Inherited from

Library.relationships

Methods

__clear()

__clear(): Promise<void>
Defined in: modules/input/src/controller/controller.library.ts:64 Called once after the tick loop stops.

Returns

Promise<void>

Overrides

Library.__clear

__events()

__events(_ctx): Promise<void>
Defined in: packages/common/dist/index.d.ts:521 Called every tick, in run order, whether or not the tick loop is paused — unlike __run. Use this for event/message draining that must not be affected by pause (e.g. listening for the command that resumes the loop), not for gameplay simulation.

Parameters

_ctx
Context

Returns

Promise<void>

Inherited from

Library.__events

__init()

__init(): Promise<void>
Defined in: modules/input/src/controller/controller.library.ts:52 Called once, in dependency order, before the tick loop starts.

Returns

Promise<void>

Overrides

Library.__init

__run()

__run(): Promise<void>
Defined in: modules/input/src/controller/controller.library.ts:59 Called every tick, in run order, while the tick loop is active and not paused.

Returns

Promise<void>

Overrides

Library.__run

expose()

expose(): ControllerContextApi
Defined in: modules/input/src/controller/controller.library.ts:143 Returns this library’s public contribution to Context[this.key].

Returns

ControllerContextApi

Remarks

Return a plain object exposing only what game code should be able to reach — never this — so internal state and lifecycle hooks never leak onto Context. Libraries with nothing to expose (headless libraries) can leave this unimplemented; they simply won’t appear on Context.

Overrides

Library.expose

getAxis()

getAxis(axis, index?): number
Defined in: modules/input/src/controller/controller.library.ts:104

Parameters

axis
ControllerAxis
index?
number = 0

Returns

number The axis value between -1 and 1, with the deadzone applied.

getButtonValue()

getButtonValue(button, index?): number
Defined in: modules/input/src/controller/controller.library.ts:98

Parameters

button
ControllerButton
index?
number = 0

Returns

number The analog value of the button, between 0 and 1.

getController()

getController(index?): ControllerState | undefined
Defined in: modules/input/src/controller/controller.library.ts:73

Parameters

index?
number = 0

Returns

ControllerState | undefined

getControllers()

getControllers(): ControllerState[]
Defined in: modules/input/src/controller/controller.library.ts:68

Returns

ControllerState[]

getStick()

getStick(stick, index?): StickPosition
Defined in: modules/input/src/controller/controller.library.ts:110

Parameters

stick
ControllerStick
index?
number = 0

Returns

StickPosition The stick position, with a radial deadzone applied.

isButtonJustPressed()

isButtonJustPressed(button, index?): boolean
Defined in: modules/input/src/controller/controller.library.ts:87

Parameters

button
ControllerButton
index?
number = 0

Returns

boolean

isButtonJustReleased()

isButtonJustReleased(button, index?): boolean
Defined in: modules/input/src/controller/controller.library.ts:92

Parameters

button
ControllerButton
index?
number = 0

Returns

boolean

isButtonPressed()

isButtonPressed(button, index?): boolean
Defined in: modules/input/src/controller/controller.library.ts:82

Parameters

button
ControllerButton
index?
number = 0

Returns

boolean

isConnected()

isConnected(index?): boolean
Defined in: modules/input/src/controller/controller.library.ts:78

Parameters

index?
number = 0

Returns

boolean

onConnect()

onConnect(listener): () => void
Defined in: modules/input/src/controller/controller.library.ts:132

Parameters

listener
ControllerListener

Returns

A function that removes the listener. () => void

onDisconnect()

onDisconnect(listener): () => void
Defined in: modules/input/src/controller/controller.library.ts:138

Parameters

listener
ControllerListener

Returns

A function that removes the listener. () => void

throwNotInitializedError()

protected throwNotInitializedError(): never
Defined in: packages/common/dist/index.d.ts:536

Returns

never

Inherited from

Library.throwNotInitializedError

vibrate()

vibrate(effect, index?): Promise<boolean>
Defined in: modules/input/src/controller/controller.library.ts:121 Plays a rumble effect.

Parameters

effect
ControllerVibration
index?
number = 0

Returns

Promise<boolean> true if the effect played to completion, false if the controller has no vibration support or the effect was interrupted.