Phaser API Documentation

  Version: 
Filter
This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

Phaser . Input . Keyboard . Key

This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

Phaser . Input . Keyboard . Key

A generic Key object which can be passed to the Process functions (and so on) keycode must be an integer

Constructor:

new Key(plugin, keyCode)

Parameters:

name type description
plugin Phaser.Input.Keyboard.KeyboardPlugin

The Keyboard Plugin instance that owns this Key object.

keyCode number

The keycode of this key.

Since: 3.0.0

Extends


Members

<private> _justDown: boolean
Focus
Focus

Description:

True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
<private> _justUp: boolean
Focus
Focus

Description:

True if the key has just been pressed (NOTE: requires to be reset, see justDown getter)

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
<private> _tick: number
Focus
Focus

Description:

Internal tick counter.

Type:
number
Since: 3.11.0
Focus
Focus
altKey: boolean
Focus
Focus

Description:

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

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
ctrlKey: boolean
Focus
Focus

Description:

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

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
duration: number
Focus
Focus

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.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
emitOnRepeat: boolean
Focus
Focus

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.

Type:
boolean
Default: false
Since: 3.16.0
Focus
Focus
enabled: boolean
Focus
Focus

Description:

Can this Key be processed?

Type:
boolean
Default: true
Since: 3.0.0
Focus
Focus
isDown: boolean
Focus
Focus

Description:

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

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
isUp: boolean
Focus
Focus

Description:

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

Type:
boolean
Default: true
Since: 3.0.0
Focus
Focus
keyCode: number
Focus
Focus

Description:

The keycode of this key.

Type:
number
Since: 3.0.0
Focus
Focus
location: number
Focus
Focus

Description:

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

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
metaKey: boolean
Focus
Focus

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.

Type:
boolean
Default: false
Since: 3.16.0
Focus
Focus
originalEvent: KeyboardEvent
Focus
Focus

Description:

The original DOM event.

Type:
KeyboardEvent
Since: 3.0.0
Focus
Focus

Description:

The Keyboard Plugin instance that owns this Key object.

Type:
Since: 3.17.0
Focus
Focus
repeats: number
Focus
Focus

Description:

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

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
shiftKey: boolean
Focus
Focus

Description:

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

Type:
boolean
Default: false
Since: 3.0.0
Focus
Focus
timeDown: number
Focus
Focus

Description:

The timestamp when the key was last pressed down.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus
timeUp: number
Focus
Focus

Description:

The timestamp when the key was last released.

Type:
number
Default: 0
Since: 3.0.0
Focus
Focus

Methods

addListener(event, fn, [context])
Focus
Focus

Description:

Add a listener for a given event.

Parameters:

name type arguments Default description
event string | symbol

The event name.

fn function

The listener function.

context * <optional> this

The context to invoke the listener with.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#addListener
Examples:
Since: 3.0.0
Focus
Focus
destroy()
Focus
Focus

Description:

Removes any bound event handlers and removes local references.

Overrides: Phaser.Events.EventEmitter#destroy
Examples:
Since: 3.16.0
Focus
Focus
emit(event, [args])
Focus
Focus

Description:

Calls each of the listeners registered for a given event.

Parameters:

name type arguments description
event string | symbol

The event name.

args * <optional>

Additional arguments that will be passed to the event handler.

Returns:
Description:

true if the event had listeners, else false.

Type:
  • boolean
Inherited from: Phaser.Events.EventEmitter#emit
Examples:
Since: 3.0.0
Focus
Focus
eventNames()
Focus
Focus

Description:

Return an array listing the events for which the emitter has registered listeners.

Type:
  • Array.<(string
  • symbol)>
Inherited from: Phaser.Events.EventEmitter#eventNames
Examples:
Since: 3.0.0
Focus
Focus
getDuration()
Focus
Focus

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.

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

Returns:
Description:

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

Type:
  • number
Examples:
Since: 3.17.0
Focus
Focus
listenerCount(event)
Focus
Focus

Description:

Return the number of listeners listening to a given event.

Parameters:

name type description
event string | symbol

The event name.

Returns:
Description:

The number of listeners.

Type:
  • number
Inherited from: Phaser.Events.EventEmitter#listenerCount
Examples:
Since: 3.0.0
Focus
Focus
listeners(event)
Focus
Focus

Description:

Return the listeners registered for a given event.

Parameters:

name type description
event string | symbol

The event name.

Returns:
Description:

The registered listeners.

Type:
  • Array.
Inherited from: Phaser.Events.EventEmitter#listeners
Examples:
Since: 3.0.0
Focus
Focus
off(event, [fn], [context], [once])
Focus
Focus

Description:

Remove the listeners of a given event.

Parameters:

name type arguments description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#off
Examples:
Since: 3.0.0
Focus
Focus
on(event, fn, [context])
Focus
Focus

Description:

Add a listener for a given event.

Parameters:

name type arguments Default description
event string | symbol

The event name.

fn function

The listener function.

context * <optional> this

The context to invoke the listener with.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#on
Examples:
Since: 3.0.0
Focus
Focus
onDown(event)
Focus
Focus

Description:

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

Parameters:

name type description
event KeyboardEvent

The native DOM Keyboard event.

Examples:
Since: 3.16.0
Focus
Focus
onUp(event)
Focus
Focus

Description:

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

Parameters:

name type description
event KeyboardEvent

The native DOM Keyboard event.

Examples:
Since: 3.16.0
Focus
Focus
once(event, fn, [context])
Focus
Focus

Description:

Add a one-time listener for a given event.

Parameters:

name type arguments Default description
event string | symbol

The event name.

fn function

The listener function.

context * <optional> this

The context to invoke the listener with.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#once
Examples:
Since: 3.0.0
Focus
Focus
removeAllListeners([event])
Focus
Focus

Description:

Remove all listeners, or those of the specified event.

Parameters:

name type arguments description
event string | symbol <optional>

The event name.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#removeAllListeners
Examples:
Since: 3.0.0
Focus
Focus
removeListener(event, [fn], [context], [once])
Focus
Focus

Description:

Remove the listeners of a given event.

Parameters:

name type arguments description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Returns:
Description:

this.

Inherited from: Phaser.Events.EventEmitter#removeListener
Examples:
Since: 3.0.0
Focus
Focus
reset()
Focus
Focus

Description:

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

Returns:
Description:

This Key instance.

Examples:
Since: 3.6.0
Focus
Focus
setEmitOnRepeat(value)
Focus
Focus

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:

name type description
value boolean

Emit down events on repeated key down actions, or just once?

Returns:
Description:

This Key instance.

Examples:
Since: 3.16.0
Focus
Focus
shutdown()
Focus
Focus

Description:

Removes all listeners.

Inherited from: Phaser.Events.EventEmitter#shutdown
Examples:
Since: 3.0.0
Focus
Focus