Skip to content

1681-engine / index / Keyboard

Class: Keyboard ​

Defined in: src/input/Keyboard.ts:11

Which keys are held, by KeyboardEvent.code (the physical key, so WASD stays WASD on any layout). pressed and released report edges since the last endStep; call that once per simulation step so a tap shorter than a frame is still seen by exactly one step.

It only knows keys pressed after it was made: a key already held when it is created (the Enter that started a level, held into it) is not down here until it is pressed again. A scene that makes its own keyboard in a field therefore starts with every key up, which is usually what a new screen wants; share one keyboard across scenes when a held key must carry over. Modifier keys are ordinary codes here (ShiftLeft); Actions reads chords from them.

Constructors ​

Constructor ​

ts
new Keyboard(target?): Keyboard;

Defined in: src/input/Keyboard.ts:17

Parameters ​

ParameterTypeDefault value
targetHTMLElement | Windowwindow

Returns ​

Keyboard

Methods ​

anyDown() ​

ts
anyDown(...codes): boolean;

Defined in: src/input/Keyboard.ts:49

Any of these keys is held.

Parameters ​

ParameterType
...codesstring[]

Returns ​

boolean


axis() ​

ts
axis(negative, positive): number;

Defined in: src/input/Keyboard.ts:64

-1, 0 or 1 from two sets of keys: axis(['KeyA', 'ArrowLeft'], ['KeyD', 'ArrowRight']).

Parameters ​

ParameterType
negativestring[]
positivestring[]

Returns ​

number


destroy() ​

ts
destroy(): void;

Defined in: src/input/Keyboard.ts:74

Returns ​

void


endStep() ​

ts
endStep(): void;

Defined in: src/input/Keyboard.ts:69

Forgets this step's edges; held keys stay held.

Returns ​

void


isDown() ​

ts
isDown(code): boolean;

Defined in: src/input/Keyboard.ts:44

Parameters ​

ParameterType
codestring

Returns ​

boolean


pressed() ​

ts
pressed(code): boolean;

Defined in: src/input/Keyboard.ts:54

Went down since the last endStep.

Parameters ​

ParameterType
codestring

Returns ​

boolean


released() ​

ts
released(code): boolean;

Defined in: src/input/Keyboard.ts:59

Came up since the last endStep.

Parameters ​

ParameterType
codestring

Returns ​

boolean

Released under the MIT License.