Class: Key

Phaser.Input.Keyboard. Key

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


new Key(keyCode)

Parameters:
Name Type Description
keyCode integer

The keycode of this key.

Since: 3.0.0
Source: src/input/keyboard/keys/Key.js (Line 11)

Extends

Members


altKey :boolean

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

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/input/keyboard/keys/Key.js (Line 82)

ctrlKey :boolean

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

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/input/keyboard/keys/Key.js (Line 92)

duration :number

The number of milliseconds this key was held down for in the previous down - up sequence.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/input/keyboard/keys/Key.js (Line 143)

emitOnRepeat :boolean

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
Since: 3.16.0
Default Value:
  • false
Source: src/input/keyboard/keys/Key.js (Line 163)

enabled :boolean

Can this Key be processed?

Type:
  • boolean
Since: 3.0.0
Default Value:
  • true
Source: src/input/keyboard/keys/Key.js (Line 52)

isDown :boolean

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

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/input/keyboard/keys/Key.js (Line 62)

isUp :boolean

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

Type:
  • boolean
Since: 3.0.0
Default Value:
  • true
Source: src/input/keyboard/keys/Key.js (Line 72)

keyCode :integer

The keycode of this key.

Type:
  • integer
Since: 3.0.0
Source: src/input/keyboard/keys/Key.js (Line 34)

location :number

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

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/input/keyboard/keys/Key.js (Line 123)

metaKey :boolean

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
Since: 3.16.0
Default Value:
  • false
Source: src/input/keyboard/keys/Key.js (Line 112)

originalEvent :KeyboardEvent

The original DOM event.

Type:
  • KeyboardEvent
Since: 3.0.0
Source: src/input/keyboard/keys/Key.js (Line 43)

repeats :number

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

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/input/keyboard/keys/Key.js (Line 176)

shiftKey :boolean

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

Type:
  • boolean
Since: 3.0.0
Default Value:
  • false
Source: src/input/keyboard/keys/Key.js (Line 102)

timeDown :number

The timestamp when the key was last pressed down.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/input/keyboard/keys/Key.js (Line 133)

timeUp :number

The timestamp when the key was last released.

Type:
  • number
Since: 3.0.0
Default Value:
  • 0
Source: src/input/keyboard/keys/Key.js (Line 153)

Methods


addListener(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 111)
Returns:

this.

Type
Phaser.Events.EventEmitter

destroy()

Removes any bound event handlers and removes local references.

Since: 3.16.0
Overrides:
Source: src/input/keyboard/keys/Key.js (Line 342)

emit(event [, args])

Calls each of the listeners registered for a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

args * <optional>
<repeatable>

Additional arguments that will be passed to the event handler.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 86)
Returns:

true if the event had listeners, else false.

Type
boolean

eventNames()

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

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 55)
Returns:
Type
array

listenerCount(event)

Return the number of listeners listening to a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 75)
Returns:

The number of listeners.

Type
number

listeners(event)

Return the listeners registered for a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 64)
Returns:

The registered listeners.

Type
array

off(event [, fn] [, context] [, once])

Remove the listeners of a given event.

Parameters:
Name Type Argument 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.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 151)
Returns:

this.

Type
Phaser.Events.EventEmitter

on(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 98)
Returns:

this.

Type
Phaser.Events.EventEmitter

once(event, fn [, context])

Add a one-time listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 124)
Returns:

this.

Type
Phaser.Events.EventEmitter

onDown(event)

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.

Since: 3.16.0
Source: src/input/keyboard/keys/Key.js (Line 237)
Fires:

onUp(event)

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.

Since: 3.16.0
Source: src/input/keyboard/keys/Key.js (Line 281)
Fires:

removeAllListeners( [event])

Remove all listeners, or those of the specified event.

Parameters:
Name Type Argument Description
event string | symbol <optional>

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 165)
Returns:

this.

Type
Phaser.Events.EventEmitter

removeListener(event [, fn] [, context] [, once])

Remove the listeners of a given event.

Parameters:
Name Type Argument 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.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 137)
Returns:

this.

Type
Phaser.Events.EventEmitter

reset()

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

Since: 3.6.0
Source: src/input/keyboard/keys/Key.js (Line 313)
Returns:

This Key instance.

Type
Phaser.Input.Keyboard.Key

setEmitOnRepeat(value)

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?

Since: 3.16.0
Source: src/input/keyboard/keys/Key.js (Line 219)
Returns:

This Key instance.

Type
Phaser.Input.Keyboard.Key

shutdown()

Removes all listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 31)