Skip to main content
Version: Phaser v4.0.0

Key

Represents a single key on the keyboard. Key objects are created by the Keyboard Plugin via addKey() and track the state of a specific key, including whether it is currently held down, the duration it has been held, timestamps for press and release events, and repeat counts. You can poll Key objects directly in your game loop using properties like isDown and isUp, or listen for events via the on method. The keyCode must be an integer corresponding to a Phaser.Input.Keyboard.KeyCodes value.

Constructor

new Key(plugin, keyCode)

Parameters

nametypeoptionaldescription
pluginPhaser.Input.Keyboard.KeyboardPluginNoThe Keyboard Plugin instance that owns this Key object.
keyCodenumberNoThe keycode of this key.

Scope: static

Extends

Phaser.Events.EventEmitter

Source: src/input/keyboard/keys/Key.js#L11
Since: 3.0.0

Public Members

altKey

altKey: boolean

Description:

The down state of the ALT key, if pressed at the same time as this key.

Source: src/input/keyboard/keys/Key.js#L96
Since: 3.0.0


ctrlKey

ctrlKey: boolean

Description:

The down state of the CTRL key, if pressed at the same time as this key.

Source: src/input/keyboard/keys/Key.js#L106
Since: 3.0.0


duration

duration: number

Description:

The number of milliseconds this key was held down for in the previous down - up sequence. This value isn't updated every game step, only when the Key changes state. To get the current duration use the getDuration method.

Source: src/input/keyboard/keys/Key.js#L157
Since: 3.0.0


emitOnRepeat

emitOnRepeat: boolean

Description:

When a key is held down should it continuously fire the down event each time it repeats?

By default it will emit the down event just once, but if you wish to receive the event for each repeat as well, enable this property.

Source: src/input/keyboard/keys/Key.js#L179
Since: 3.16.0


enabled

enabled: boolean

Description:

Can this Key be processed?

Source: src/input/keyboard/keys/Key.js#L66
Since: 3.0.0


isDown

isDown: boolean

Description:

The "down" state of the key. This will remain true for as long as the keyboard thinks this key is held down.

Source: src/input/keyboard/keys/Key.js#L76
Since: 3.0.0


isUp

isUp: boolean

Description:

The "up" state of the key. This will remain true for as long as the keyboard thinks this key is up.

Source: src/input/keyboard/keys/Key.js#L86
Since: 3.0.0


keyCode

keyCode: number

Description:

The keycode of this key.

Source: src/input/keyboard/keys/Key.js#L48
Since: 3.0.0


location

location: number

Description:

The location of the modifier key. 0 for standard (or unknown), 1 for left, 2 for right, 3 for numpad.

Source: src/input/keyboard/keys/Key.js#L137
Since: 3.0.0


metaKey

metaKey: boolean

Description:

The down state of the Meta key, if pressed at the same time as this key. On a Mac the Meta Key is the Command key. On Windows keyboards, it's the Windows key.

Source: src/input/keyboard/keys/Key.js#L126
Since: 3.16.0


originalEvent

originalEvent: KeyboardEvent

Description:

The original DOM event.

Source: src/input/keyboard/keys/Key.js#L57
Since: 3.0.0


plugin

plugin: Phaser.Input.Keyboard.KeyboardPlugin

Description:

The Keyboard Plugin instance that owns this Key object.

Source: src/input/keyboard/keys/Key.js#L39
Since: 3.17.0


repeats

repeats: number

Description:

If a key is held down this holds the number of times the key has 'repeated'.

Source: src/input/keyboard/keys/Key.js#L192
Since: 3.0.0


shiftKey

shiftKey: boolean

Description:

The down state of the SHIFT key, if pressed at the same time as this key.

Source: src/input/keyboard/keys/Key.js#L116
Since: 3.0.0


timeDown

timeDown: number

Description:

The timestamp when the key was last pressed down.

Source: src/input/keyboard/keys/Key.js#L147
Since: 3.0.0


timeUp

timeUp: number

Description:

The timestamp when the key was last released.

Source: src/input/keyboard/keys/Key.js#L169
Since: 3.0.0


Inherited Methods

From Phaser.Events.EventEmitter:


Public Methods

destroy

<instance> destroy()

Description:

Removes any bound event handlers and removes local references.

Overrides: Phaser.Events.EventEmitter#destroy

Source: src/input/keyboard/keys/Key.js#L383
Since: 3.16.0


getDuration

<instance> getDuration()

Description:

Returns the duration, in ms, that the Key has been held down for.

If the key is not currently down it will return zero.

To get the duration the Key was held down for in the previous up-down cycle, use the Key.duration property value instead.

Returns: number - The duration, in ms, that the Key has been held down for if currently down.

Source: src/input/keyboard/keys/Key.js#L358
Since: 3.17.0


onDown

<instance> onDown(event)

Description:

Processes the Key Down action for this Key. Called automatically by the Keyboard Plugin.

Parameters:

nametypeoptionaldescription
eventKeyboardEventNoThe native DOM Keyboard event.

Fires: Phaser.Input.Keyboard.Events#event:DOWN

Source: src/input/keyboard/keys/Key.js#L253
Since: 3.16.0


onUp

<instance> onUp(event)

Description:

Processes the Key Up action for this Key. Called automatically by the Keyboard Plugin.

Parameters:

nametypeoptionaldescription
eventKeyboardEventNoThe native DOM Keyboard event.

Fires: Phaser.Input.Keyboard.Events#event:UP

Source: src/input/keyboard/keys/Key.js#L297
Since: 3.16.0


reset

<instance> reset()

Description:

Resets this Key object back to its default un-pressed state.

As of version 3.60.0 it no longer resets the enabled or preventDefault flags.

Returns: Phaser.Input.Keyboard.Key - This Key instance.

Source: src/input/keyboard/keys/Key.js#L329
Since: 3.6.0


setEmitOnRepeat

<instance> setEmitOnRepeat(value)

Description:

Controls if this Key will continuously emit a down event while being held down (true), or emit the event just once, on first press, and then skip future events (false).

Parameters:

nametypeoptionaldescription
valuebooleanNoEmit down events on repeated key down actions, or just once?

Returns: Phaser.Input.Keyboard.Key - This Key instance.

Source: src/input/keyboard/keys/Key.js#L235
Since: 3.16.0