Class
Buttons
View sourceTracks 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
Methods
press(button: T): void
View sourceMarks a button as pressed and records a just-pressed transition when it was not already pressed.
release(button: T): void
View sourceMarks a button as released and records a just-released transition only when it was currently pressed.
pressed(button: T): boolean
View sourceTests whether a button is currently held down.
anyJustPressed(...buttons: T[]): boolean
View sourceTests whether at least one of the supplied buttons has a pending just-pressed transition.
anyPressed(...buttons: T[]): boolean
View sourceTests whether at least one of the supplied buttons is currently held.
anyJustReleased(...buttons: T[]): boolean
View sourceTests whether at least one of the supplied buttons has a pending just-released transition.
justPressed(button: T): boolean
View sourceTests whether a button has a pending just-pressed transition.
justReleased(button: T): boolean
View sourceTests whether a button has a pending just-released transition.
clearJustPressed(): void
View sourceClears all pending just-pressed transitions.
This does not change which buttons are currently pressed.
clearJustReleased(): void
View sourceClears all pending just-released transitions.
This does not change which buttons are currently pressed.
clearPressed(): void
View sourceClears all currently pressed buttons.
This does not create just-released transitions or clear pending just-pressed and just-released transitions.
