Skip to main content

Class: KeyboardHandler

input/keyboard.KeyboardHandler

Handles keyboard events for an {@link HTMLElement}.

Constructors

constructor

โ€ข new KeyboardHandler(element)

Parameters

NameType
elementHTMLElement

Defined in

src/input/keyboard.ts:15

Properties

element

โ€ข Readonly element: HTMLElement

Defined in

src/input/keyboard.ts:10


keys

โ€ข keys: Object = {}

Index signature

โ–ช [index: string]: boolean

Defined in

src/input/keyboard.ts:12


listeners

โ€ข listeners: Object = {}

Index signature

โ–ช [index: string]: KeyCallback[]

Defined in

src/input/keyboard.ts:13

Methods

addListener

โ–ธ addListener(code, cb): void

Attaches a listener to a given key code that is called whenever the state of the key changes.

Parameters

NameTypeDescription
codestringThe key code to attach the listener to
cbKeyCallbackThe callback to execute on a keydown/keyup event

Returns

void

Defined in

src/input/keyboard.ts:71


addListeners

โ–ธ Private addListeners(): void

Returns

void

Defined in

src/input/keyboard.ts:24


isPressed

โ–ธ isPressed(code): boolean

Determines wether or not a key is currently pressed.

see Key Codes

Parameters

NameTypeDescription
codestringThe key code to check

Returns

boolean

Wether or not the key corresponding to the given code is pressed.

Defined in

src/input/keyboard.ts:44


removeListener

โ–ธ removeListener(code, cb): void

Removes a listener from a given key code.

Parameters

NameTypeDescription
codestringThe key code the listener is attached to
cbKeyCallbackThe attached listener

Returns

void

Defined in

src/input/keyboard.ts:85


setKey

โ–ธ setKey(code, pressed): void

Emulates a keydown/keyup event on the pages body for a given key code.

see Key Codes

Parameters

NameTypeDescription
codestringThe key code to emulate
pressedbooleanWhen true a keydown event is emulated otherwise keyup is emulated

Returns

void

Defined in

src/input/keyboard.ts:56