Keyboard

Keyboard

Keyboard Input Wrapper

To detect if a key is pressed, we use KeyboardEvent.code, since which, keyCode and charCode are deprecated. e.key would be another option but e.key could be "z" in german and english, no matter which layout but "я" in russian.

e.code represents the physical key on the keyboard, NOT the charcode generated by the event!

For example, the code returned is "KeyQ" for the Q key on a QWERTY layout keyboard, but the same code value also represents the ' key on Dvorak keyboards and the A key on AZERTY keyboards. That makes it impossible to use the value of code to determine what the name of the key is to users if they're not using an anticipated keyboard layout.

Getting the string associated to a physical key might be possible in the future using Keyboard.getLayoutMap() but this is experimental technology.

Your game should not care about which character the keypress would have caused, your game should translate from physical key to in-game action and let the user decide which physical key the want for that action via a settings page.

Choosing a reasonable default could be a QWERTY layout keyboard while avoiding keys like y and z, but in the end you do not know what kind of input device the user actually has.

Methods

destroy()

Source:

unbind the keydown / keyup events

isPressed(code) → {Boolean}

Source:

query keyboard state to know if a key is pressed of not

Parameters:
Name Type Description
code String | Array

single keycode or array of keycodes

Returns:

true if the key is pressed, false otherwise

Type
Boolean

time(code) → {Integer}

Source:

query keyboard state to know if a key is pressed of not

Parameters:
Name Type Description
code String

the description of the key. format : modifiers+key e.g shift+A

Returns:

Millisecond timestamp of the time it was last pressed or released

Type
Integer

Events

down

Source:
Properties:
Name Type Description
pressed Boolean

Indicates whether the key is pressed or not

time Number

Indicates when the keypress occured

Keydown event.

up

Source:
Properties:
Name Type Description
pressed Boolean

Indicates whether the key is pressed or not

time Number

Indicates when the keypress occured

Keyup event.