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 52
-
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 58
-
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 77
-
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 91
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 385
-
<readonly> event : object
-
Stores the most recent DOM event.
Type:
- object
- Source:
- src/input/Key.js line 34
-
game : Phaser.Game
-
A reference to the currently running game.
Type:
- Source:
- src/input/Key.js line 21
-
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 40
-
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 46
-
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 337
-
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 361
-
keyCode : number
-
The keycode of this key.
Type:
- number
- Source:
- src/input/Key.js line 102
-
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 107
-
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 112
-
onHoldContext : object
-
The context under which the onHoldCallback will be called.
Type:
- object
- Source:
- src/input/Key.js line 117
-
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 122
-
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 97
-
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 64
-
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 69
-
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 83
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 273
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 307
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 321
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 168
-
<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 207
-
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 240
-
<protected> update()
-
Called automatically by Phaser.Keyboard.
- Source:
- src/input/Key.js line 140
-
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 290
Returns:
True if the key was released within the given duration.
- Type
- boolean