# Class: Keyboard

## Phaser. Keyboard

Phaser.Keyboard

#### new Keyboard(game)

  The Keyboard class handles looking after keyboard input for your game. It will recognise and respond to key presses and dispatch the required events. Please be aware that lots of 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

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `callbackContext` | Object | The context under which the callbacks are run.

  Source:
    - input/Keyboard.js, line 39

#### disabled

  You can disable all Keyboard Input by setting disabled to true. While true all new input related events will be ignored.

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `disabled` | boolean | The disabled state of the Keyboard.

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

#### event

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `event` | Object | The most recent DOM event. This is updated every time a new key is pressed or released.

  Source:
    - input/Keyboard.js, line 34

#### game

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `game` | Phaser.Game | Local reference to game.

  Source:
    - input/Keyboard.js, line 22

#### onDownCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onDownCallback` | function | This callback is invoked every time a key is pressed down.

  Source:
    - input/Keyboard.js, line 44

#### onUpCallback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onUpCallback` | function | This callback is invoked every time a key is released.

  Source:
    - input/Keyboard.js, line 49

### Methods

#### addCallbacks(context, onDown, onUp)

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

  Source:
    - input/Keyboard.js, line 87

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

  Source:
    - input/Keyboard.js, line 107

  ##### Returns:

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

  Type
    Phaser.Key

#### 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` | Any | Either a single numeric keycode or an array/hash of keycodes: [65, 67, 68].

  Source:
    - input/Keyboard.js, line 223

#### clearCaptures()

  Clear all set key captures.

  Source:
    - input/Keyboard.js, line 259

#### createCursorKeys() → {object}

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

  Source:
    - input/Keyboard.js, line 145

  ##### Returns:

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

  Type
    object

#### 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 206

#### 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 remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR

  Source:
    - input/Keyboard.js, line 430

  ##### Returns:

  True if the key is currently down.

  Type
    boolean

#### justPressed(keycode, duration) → {boolean}

  Returns the "just pressed" state of the key. Just pressed is considered true if the key was pressed down within the duration given (default 250ms)

  ##### Parameters:

  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `keycode` | number |  |  | The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
  `duration` | number | <optional> | 50 | The duration below which the key is considered as being just pressed.

  Source:
    - input/Keyboard.js, line 384

  ##### Returns:

  True if the key is just pressed otherwise false.

  Type
    boolean

#### justReleased(keycode, duration) → {boolean}

  Returns the "just released" state of the Key. Just released is considered as being true if the key was released within the duration given (default 250ms)

  ##### Parameters:

  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `keycode` | number |  |  | The keycode of the key to remove, i.e. Phaser.Keyboard.UP or Phaser.Keyboard.SPACEBAR
  `duration` | number | <optional> | 50 | The duration below which the key is considered as being just released.

  Source:
    - input/Keyboard.js, line 407

  ##### Returns:

  True if the key is just released otherwise false.

  Type
    boolean

#### <protected> processKeyDown(event)

  Process the keydown event.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `event` | KeyboardEvent |

  Source:
    - input/Keyboard.js, line 289

#### <protected> processKeyUp(event)

  Process the keyup event.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `event` | KeyboardEvent |

  Source:
    - input/Keyboard.js, line 325

#### 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 128

#### removeKeyCapture(keycode)

  Removes an existing key capture.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `keycode` | number |

  Source:
    - input/Keyboard.js, line 247

#### 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 360

#### 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 162

#### stop()

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

  Source:
    - input/Keyboard.js, line 191

#### update()

  Updates all currently defined keys.

  Source:
    - input/Keyboard.js, line 270