Navigation
These archived docs are for Phaser 2.4.3 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. Key

new Key(game, keycode)

If you need more fine-grained control over the handling of specific keys you can create and use Phaser.Key objects.

Parameters
Name Type Description
game Phaser.Game

Current game instance.

keycode number

The key code this Key is responsible for.

Source code: input/Key.js (Line 15)

Public Properties

altKey : boolean

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

Source code: input/Key.js (Line 51)

ctrlKey : boolean

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

Source code: input/Key.js (Line 57)

duration : number

If the key is down this value holds the duration of that key press and is constantly updated. If the key is up it holds the duration of the previous down session. The number of milliseconds this key has been held down for.

Source code: input/Key.js (Line 76)

[readonly] event : Object

Stores the most recent DOM event.

Source code: input/Key.js (Line 33)

game : Phaser.Game

A reference to the currently running game.

Source code: input/Key.js (Line 20)

isDown : boolean

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

Source code: input/Key.js (Line 39)

isUp : boolean

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

Default Value
  • true
Source code: input/Key.js (Line 45)

keyCode : number

The keycode of this key.

Source code: input/Key.js (Line 93)

onDown : Phaser.Signal

This Signal is dispatched every time this Key is pressed down. It is only dispatched once (until the key is released again).

Source code: input/Key.js (Line 98)

onHoldCallback : Function

A callback that is called while this Key is held down. Warning: Depending on refresh rate that could be 60+ times per second.

Source code: input/Key.js (Line 103)

onHoldContext : Object

The context under which the onHoldCallback will be called.

Source code: input/Key.js (Line 108)

onUp : Phaser.Signal

This Signal is dispatched every time this Key is released. It is only dispatched once (until the key is pressed and released again).

Source code: input/Key.js (Line 113)

repeats : number

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

Source code: input/Key.js (Line 88)

shiftKey : boolean

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

Source code: input/Key.js (Line 63)

timeDown : number

The timestamp when the key was last pressed down. This is based on Game.time.now.

Source code: input/Key.js (Line 68)

timeUp : number

The timestamp when the key was last released. This is based on Game.time.now.

Source code: input/Key.js (Line 82)

Public Methods

downDuration(duration) → {boolean}

Returns true if the Key was pressed down within the duration value given, or false if it either isn't down, or was pressed down longer ago than then given duration.

Parameters
Name Type Argument Default Description
duration number <optional>
50

The duration within which the key is considered as being just pressed. Given in ms.

Returns
boolean -

True if the key was pressed down within the given duration.

Source code: input/Key.js (Line 253)

<internal> processKeyDown(event)

Called automatically by Phaser.Keyboard.

Parameters
Name Type Description
event KeyboardEvent

The DOM event that triggered this.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: input/Key.js (Line 154)

<internal> processKeyUp(event)

Called automatically by Phaser.Keyboard.

Parameters
Name Type Description
event KeyboardEvent

The DOM event that triggered this.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: input/Key.js (Line 191)

reset(hard)

Resets the state of this Key.

This sets isDown to false, isUp to true, resets the time to be the current time, and enables the key. In addition, if it is a "hard reset", it clears clears any callbacks associated with the onDown and onUp events and removes the onHoldCallback.

Parameters
Name Type Argument Default Description
hard boolean <optional>
true

A soft reset won't reset any events or callbacks; a hard reset will.

Source code: input/Key.js (Line 222)

<internal> update()

Called automatically by Phaser.Keyboard.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: input/Key.js (Line 131)

upDuration(duration) → {boolean}

Returns true if the Key was pressed down within the duration value given, or false if it either isn't down, or was pressed down longer ago than then given duration.

Parameters
Name Type Argument Default Description
duration number <optional>
50

The duration within which the key is considered as being just released. Given in ms.

Returns
boolean -

True if the key was released within the given duration.

Source code: input/Key.js (Line 269)