Phaser. Keyboard

new Keyboard(game)

The Keyboard class monitors keyboard input and dispatches keyboard events.

Be aware that many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting. Full details here: http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/

Parameters:
Name Type Description
game Phaser.Game

A reference to the currently running game.

Source - input/Keyboard.js, line 17

Members

callbackContext :object

The context under which the callbacks are run.

Source - input/Keyboard.js, line 44

enabled :boolean

Keyboard input will only be processed if enabled.

Default Value:
  • true
Source - input/Keyboard.js, line 29

event :object

The most recent DOM event from keydown or keyup. This is updated every time a new key is pressed or released.

Source - input/Keyboard.js, line 34

game :Phaser.Game

Local reference to game.

Source - input/Keyboard.js, line 22

<readonly> lastChar :string

Returns the string value of the most recently pressed key.

Source - input/Keyboard.js, line 539

<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.

Source - input/Keyboard.js, line 562

onDownCallback :function

This callback is invoked every time a key is pressed down, including key repeats when a key is held down.

Source - input/Keyboard.js, line 49

onPressCallback :function

This callback is invoked every time a DOM onkeypress event is raised, which is only for printable keys.

Source - input/Keyboard.js, line 54

onUpCallback :function

This callback is invoked every time a key is released.

Source - input/Keyboard.js, line 59

pressEvent :object

The most recent DOM event from keypress.

Source - input/Keyboard.js, line 39

Methods

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.

Source - input/Keyboard.js, line 110

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 number

The keycode of the key, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR

Returns:

The Key object which you can store locally and reference directly.

Source - input/Keyboard.js, line 140

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. You can use addKeyCapture to consume the keyboard event for specific keys so it doesn't bubble up to the the browser. Pass in either a single keycode or an array/hash of keycodes.

Parameters:
Name Type Description
keycode number | array | object

Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].

Source - input/Keyboard.js, line 287

addKeys(keys) → {object}

A practical way to create an object containing user selected hotkeys.

For example: addKeys( { 'up': Phaser.Keyboard.W, 'down': Phaser.Keyboard.S, 'left': Phaser.Keyboard.A, 'right': Phaser.Keyboard.D } );

Would return an object containing the properties up, down, left and right that you could poll just like a Phaser.Key object.

Parameters:
Name Type Description
keys object

A key mapping object, i.e. { 'up': Phaser.Keyboard.W, 'down': Phaser.Keyboard.S } or { 'up': 52, 'down': 53 }.

Returns:
object -

An object containing user selected properties

Source - input/Keyboard.js, line 161

clearCaptures()

Clear all set key captures.

Source - input/Keyboard.js, line 323

createCursorKeys() → {object}

Creates and returns an object containing 4 hotkeys for Up, Down, Left and Right.

Returns:
object -

An object containing properties: up, down, left and right. Which can be polled like any other Phaser.Key object.

Source - input/Keyboard.js, line 202

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.

Source - input/Keyboard.js, line 270

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 number

The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR

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, false if not or null if the Key wasn't found.

Source - input/Keyboard.js, line 473

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 number

The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR

Returns:
boolean -

True if the key is currently down, false if not or null if the Key wasn't found.

Source - input/Keyboard.js, line 517

<internal> processKeyDown(event)

Process the keydown event.

Parameters:
Name Type Description
event KeyboardEvent
Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source - input/Keyboard.js, line 353

<internal> processKeyPress(event)

Process the keypress event.

Parameters:
Name Type Description
event KeyboardEvent
Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source - input/Keyboard.js, line 391

<internal> processKeyUp(event)

Process the keyup event.

Parameters:
Name Type Description
event KeyboardEvent
Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source - input/Keyboard.js, line 414

removeKey(keycode)

Removes a Key object from the Keyboard manager.

Parameters:
Name Type Description
keycode number

The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR

Source - input/Keyboard.js, line 185

removeKeyCapture(keycode)

Removes an existing key capture.

Parameters:
Name Type Description
keycode number
Source - input/Keyboard.js, line 311

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.

Source - input/Keyboard.js, line 449

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.

Source - input/Keyboard.js, line 214

stop()

Stops the Keyboard event listeners from running (keydown, keyup and keypress). They are removed from the window.

Source - input/Keyboard.js, line 253

update()

Updates all currently defined keys.

Source - input/Keyboard.js, line 334

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 number

The keycode of the key to check, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR

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, false if not or null if the Key wasn't found.

Source - input/Keyboard.js, line 495
Phaser Copyright © 2012-2015 Photon Storm Ltd.
Documentation generated by JSDoc 3.3.2 on Mon Aug 24 2015 15:36:50 GMT+0100 (GMT Daylight Time) using the DocStrap template.