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 |
integer | The key code this Key is responsible for. See Phaser.KeyCode. |
- Source:
- src/input/Key.js line 15
Members
-
altKey : boolean
-
The down state of the ALT key, if pressed at the same time as this key.
Type:
- boolean
- Source:
- src/input/Key.js line 51
-
ctrlKey : boolean
-
The down state of the CTRL key, if pressed at the same time as this key.
Type:
- boolean
- Source:
- src/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.
Type:
- number
- Source:
- src/input/Key.js line 76
-
durationUp
-
If the key is up this value holds the duration of that key release and is constantly updated. If the key is down it holds the duration of the previous up session.
- Source:
- src/input/Key.js line 90
Properties:
Name Type Description duration
number The number of milliseconds this key has been up for.
-
enabled : boolean
-
An enabled key processes its update and dispatches events. A key can be disabled momentarily at runtime instead of deleting it.
Type:
- boolean
- Default Value:
-
- true
- Source:
- src/input/Key.js line 367
-
<readonly> event : object
-
Stores the most recent DOM event.
Type:
- object
- Source:
- src/input/Key.js line 33
-
game : Phaser.Game
-
A reference to the currently running game.
Type:
- Source:
- src/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.Type:
- boolean
- Source:
- src/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.Type:
- boolean
- Default Value:
-
- true
- Source:
- src/input/Key.js line 45
-
justDown : boolean
-
The justDown value allows you to test if this Key has just been pressed down or not. When you check this value it will return
true
if the Key is down, otherwisefalse
. You can only call justDown once per key press. It will only returntrue
once, until the Key is released and pressed down again. This allows you to use it in situations where you want to check if this key is down without using a Signal, such as in a core game loop.Type:
- boolean
- Default Value:
-
- false
- Source:
- src/input/Key.js line 323
-
justUp : boolean
-
The justUp value allows you to test if this Key has just been released or not. When you check this value it will return
true
if the Key is up, otherwisefalse
. You can only call justUp once per key release. It will only returntrue
once, until the Key is pressed down and released again. This allows you to use it in situations where you want to check if this key is up without using a Signal, such as in a core game loop.Type:
- boolean
- Default Value:
-
- false
- Source:
- src/input/Key.js line 345
-
keyCode : number
-
The keycode of this key.
Type:
- number
- Source:
- src/input/Key.js line 101
-
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).
Type:
- Source:
- src/input/Key.js line 106
-
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.
Type:
- function
- Source:
- src/input/Key.js line 111
-
onHoldContext : object
-
The context under which the onHoldCallback will be called.
Type:
- object
- Source:
- src/input/Key.js line 116
-
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).
Type:
- Source:
- src/input/Key.js line 121
-
repeats : number
-
If a key is held down this holds down the number of times the key has 'repeated'.
Type:
- number
- Source:
- src/input/Key.js line 96
-
shiftKey : boolean
-
The down state of the SHIFT key, if pressed at the same time as this key.
Type:
- boolean
- Source:
- src/input/Key.js line 63
-
timeDown : number
-
The timestamp when the key was last pressed down. This is based on Game.time.now.
Type:
- number
- Source:
- src/input/Key.js line 68
-
timeUp : number
-
The timestamp when the key was last released. This is based on Game.time.now.
Type:
- number
- Source:
- src/input/Key.js line 82
Methods
-
downDuration( [duration])
-
Returns
true
if the Key was pressed down within theduration
value given, orfalse
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.
- Source:
- src/input/Key.js line 267
Returns:
True if the key was pressed down within the given duration.
- Type
- boolean
-
justPressed()
-
Returns
true
if the Key was just pressed down this update tick, orfalse
if it either isn't down, or was pressed down on a previous update tick.- Source:
- src/input/Key.js line 297
Returns:
True if the key was just pressed down this update tick.
- Type
- boolean
-
justReleased()
-
Returns
true
if the Key was just released this update tick, orfalse
if it either isn't up, or was released on a previous update tick.- Source:
- src/input/Key.js line 309
Returns:
True if the key was just released this update tick.
- Type
- boolean
-
<protected> processKeyDown(event)
-
Called automatically by Phaser.Keyboard.
Parameters:
Name Type Description event
KeyboardEvent The DOM event that triggered this.
- Source:
- src/input/Key.js line 164
-
<protected> processKeyUp(event)
-
Called automatically by Phaser.Keyboard.
Parameters:
Name Type Description event
KeyboardEvent The DOM event that triggered this.
- Source:
- src/input/Key.js line 203
-
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:
- src/input/Key.js line 236
-
<protected> update()
-
Called automatically by Phaser.Keyboard.
- Source:
- src/input/Key.js line 138
-
upDuration( [duration])
-
Returns
true
if the Key has been up only within theduration
value given, orfalse
if it either isn't up, or was has been up longer than the 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.
- Source:
- src/input/Key.js line 282
Returns:
True if the key was released within the given duration.
- Type
- boolean