Buttons

View source

Tracks held buttons and pending press and release transitions for input.

This generic utility accepts any token type T that identifies a button, including keyboard codes, pointer buttons, and gamepad inputs. Record transitions with Buttons#press and Buttons#release, then query current and transition state while processing input.

Pressing an already pressed button does not create another just-pressed transition. Releasing a button that is not pressed has no effect. The just-pressed and just-released states persist until their corresponding clear operation is called; they are not cleared automatically.

Examples

const buttons = new Buttons()
buttons.press('jump')
if (buttons.justPressed('jump')) {
  startJump()
}
buttons.clearJustPressed()

Constructor


constructor<T>()

Type Parameters

T

Methods


press(button: T): void

View source

Marks a button as pressed and records a just-pressed transition when it was not already pressed.

Parameters

NameDescriptionDefault
button——

release(button: T): void

View source

Marks a button as released and records a just-released transition only when it was currently pressed.

Parameters

NameDescriptionDefault
button——

pressed(button: T): boolean

View source

Tests whether a button is currently held down.

Parameters

NameDescriptionDefault
button——

anyJustPressed(...buttons: T[]): boolean

View source

Tests whether at least one of the supplied buttons has a pending just-pressed transition.

Parameters

NameDescriptionDefault
...buttons——

anyPressed(...buttons: T[]): boolean

View source

Tests whether at least one of the supplied buttons is currently held.

Parameters

NameDescriptionDefault
...buttons——

anyJustReleased(...buttons: T[]): boolean

View source

Tests whether at least one of the supplied buttons has a pending just-released transition.

Parameters

NameDescriptionDefault
...buttons——

justPressed(button: T): boolean

View source

Tests whether a button has a pending just-pressed transition.

Parameters

NameDescriptionDefault
button——

justReleased(button: T): boolean

View source

Tests whether a button has a pending just-released transition.

Parameters

NameDescriptionDefault
button——

clearJustPressed(): void

View source

Clears all pending just-pressed transitions.

This does not change which buttons are currently pressed.

clearJustReleased(): void

View source

Clears all pending just-released transitions.

This does not change which buttons are currently pressed.

clearPressed(): void

View source

Clears all currently pressed buttons.

This does not create just-released transitions or clear pending just-pressed and just-released transitions.