Class: Phaser.Keyboard
Constructor
new Keyboard(game)
The Keyboard class monitors keyboard input and dispatches keyboard events.
Note: many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
See http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/ for more details.
Also please be aware that certain browser extensions can disable or override Phaser keyboard handling.
For example the Chrome extension vimium is known to disable Phaser from using the D key. And there are others.
So please check your extensions before opening Phaser issues.
Parameters
Name | Type | Description |
---|---|---|
game | Phaser.Game | A reference to the currently running game. |
- Source code: input/Keyboard.js (Line 21)
Public Properties
- Source code: input/Keyboard.js (Line 48)
- Default Value
- true
- Source code: input/Keyboard.js (Line 33)
- Source code: input/Keyboard.js (Line 38)
- Source code: input/Keyboard.js (Line 26)
- Source code: input/Keyboard.js (Line 553)
- Source code: input/Keyboard.js (Line 576)
- Source code: input/Keyboard.js (Line 53)
- Source code: input/Keyboard.js (Line 58)
- Source code: input/Keyboard.js (Line 63)
- Source code: input/Keyboard.js (Line 43)
callbackContext : Object
The context under which the callbacks are run.
enabled : boolean
Keyboard input will only be processed if enabled.
event : Object
The most recent DOM event from keydown or keyup. This is updated every time a new key is pressed or released.
game : Phaser.Game
Local reference to game.
[readonly] lastChar : string
Returns the string value of the most recently pressed key.
[readonly] lastKey : Phaser.Key
Returns the most recently pressed Key. This is a Phaser.Key object and it changes every time a key is pressed.
onDownCallback : Function
This callback is invoked every time a key is pressed down, including key repeats when a key is held down.
onPressCallback : Function
This callback is invoked every time a DOM onkeypress event is raised, which is only for printable keys.
onUpCallback : Function
This callback is invoked every time a key is released.
pressEvent : Object
The most recent DOM event from keypress.
Public Methods
- Source code: input/Keyboard.js (Line 114)
- Source code: input/Keyboard.js (Line 144)
- Source code: input/Keyboard.js (Line 294)
- Source code: input/Keyboard.js (Line 165)
- Source code: input/Keyboard.js (Line 333)
- Source code: input/Keyboard.js (Line 208)
- Source code: input/Keyboard.js (Line 277)
- Source code: input/Keyboard.js (Line 487)
- Source code: input/Keyboard.js (Line 531)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: input/Keyboard.js (Line 363)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: input/Keyboard.js (Line 403)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: input/Keyboard.js (Line 426)
- Source code: input/Keyboard.js (Line 191)
- Source code: input/Keyboard.js (Line 321)
- Source code: input/Keyboard.js (Line 463)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: input/Keyboard.js (Line 220)
- Source code: input/Keyboard.js (Line 260)
- Source code: input/Keyboard.js (Line 344)
- Source code: input/Keyboard.js (Line 509)
addCallbacks(context, onDown, onUp, onPress)
Add callbacks to the Keyboard handler so that each time a key is pressed down or released the callbacks are activated.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
context | object | The context under which the callbacks are run. | ||
onDown | function | <optional> | null | This callback is invoked every time a key is pressed down. |
onUp | function | <optional> | null | This callback is invoked every time a key is released. |
onPress | function | <optional> | null | This callback is invoked every time the onkeypress event is raised. |
addKey(keycode) → {Phaser.Key}
If you need more fine-grained control over a Key you can create a new Phaser.Key object via this method.
The Key object can then be polled, have events attached to it, etc.
Parameters
Name | Type | Description |
---|---|---|
keycode | integer | The keycode of the key. |
Returns
The Key object which you can store locally and reference directly.
addKeyCapture(keycode)
By default when a key is pressed Phaser will not stop the event from propagating up to the browser.
There are some keys this can be annoying for, like the arrow keys or space bar, which make the browser window scroll.
The addKeyCapture
method enables consuming keyboard event for specific keys so it doesn't bubble up to the the browser
and cause the default browser behavior.
Pass in either a single keycode or an array/hash of keycodes.
Parameters
Name | Type | Description |
---|---|---|
keycode | integer | Array.<integer> | object | Either a single keycode or an array/hash of keycodes such as |
addKeys(keys) → {object}
A practical way to create an object containing user selected hotkeys.
For example,
addKeys( { 'up': Phaser.KeyCode.W, 'down': Phaser.KeyCode.S, 'left': Phaser.KeyCode.A, 'right': Phaser.KeyCode.D } );
would return an object containing properties (up
, down
, left
and right
) referring to Phaser.Key object.
Parameters
Name | Type | Description |
---|---|---|
keys | object | A key mapping object, i.e. |
Returns
An object containing the properties mapped to Phaser.Key values.
clearCaptures()
Clear all set key captures.
createCursorKeys() → {object}
Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right.
Returns
An object containing properties: up
, down
, left
and right
of Phaser.Key objects.
destroy()
Stops the Keyboard event listeners from running (keydown and keyup). They are removed from the window.
Also clears all key captures and currently created Key objects.
downDuration(keycode, 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 |
---|---|---|---|---|
keycode | integer | The keycode of the key to check: i.e. Phaser.KeyCode.UP or Phaser.KeyCode.SPACEBAR. | ||
duration | number | <optional> | 50 | The duration within which the key is considered as being just pressed. Given in ms. |
Returns
True if the key was pressed down within the given duration, false if not or null if the Key wasn't found.
isDown(keycode) → {boolean}
Returns true of the key is currently pressed down. Note that it can only detect key presses on the web browser.
Parameters
Name | Type | Description |
---|---|---|
keycode | integer | The keycode of the key to check: i.e. Phaser.KeyCode.UP or Phaser.KeyCode.SPACEBAR. |
Returns
True if the key is currently down, false if not or null if the Key wasn't found.
<internal> processKeyDown(event)
Process the keydown event.
Parameters
Name | Type | Description |
---|---|---|
event | KeyboardEvent |
<internal> processKeyPress(event)
Process the keypress event.
Parameters
Name | Type | Description |
---|---|---|
event | KeyboardEvent |
<internal> processKeyUp(event)
Process the keyup event.
Parameters
Name | Type | Description |
---|---|---|
event | KeyboardEvent |
removeKey(keycode)
Removes a Key object from the Keyboard manager.
Parameters
Name | Type | Description |
---|---|---|
keycode | integer | The keycode of the key to remove. |
removeKeyCapture(keycode)
Removes an existing key capture.
Parameters
Name | Type | Description |
---|---|---|
keycode | integer | The keycode to remove capturing of. |
reset(hard)
Resets all Keys.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
hard | boolean | <optional> | true | A soft reset won't reset any events or callbacks that are bound to the Keys. A hard reset will. |
<internal> start()
Starts the Keyboard event listeners running (keydown and keyup). They are attached to the window.
This is called automatically by Phaser.Input and should not normally be invoked directly.
stop()
Stops the Keyboard event listeners from running (keydown, keyup and keypress). They are removed from the window.
update()
Updates all currently defined keys.
upDuration(keycode, 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 |
---|---|---|---|---|
keycode | Phaser.KeyCode | integer | The keycode of the key to check, i.e. Phaser.KeyCode.UP or Phaser.KeyCode.SPACEBAR. | ||
duration | number | <optional> | 50 | The duration within which the key is considered as being just released. Given in ms. |
Returns
True if the key was released within the given duration, false if not or null if the Key wasn't found.
© 2016 Richard Davey, Photon Storm Ltd.
Licensed under the MIT License.
http://phaser.io/docs/2.6.2/Phaser.Keyboard.html