• Public
  • Public/Protected
  • All

Class Keyboard

Provides keyboard support for Excalibur.

Working with the keyboard is easy in Excalibur. You can inspect whether a button was just pressed or released this frame, or if the key is currently being held down. Common keys are held in the Input.Keys enumeration but you can pass any character code to the methods.

Excalibur subscribes to the browser events and keeps track of what keys are currently held, released, or pressed. A key can be held for multiple frames, but a key cannot be pressed or released for more than one subsequent update frame.

Inspecting the keyboard

You can inspect Engine.input to see what the state of the keyboard is during an update.

It is recommended that keyboard actions that directly effect actors be handled like so to improve code quality:

class Player extends ex.Actor {
  public update(engine, delta) {

    if (engine.input.keyboard.isHeld(ex.Input.Keys.W) ||
        engine.input.keyboard.isHeld(ex.Input.Keys.Up)) {


    if (engine.input.keyboard.wasPressed(ex.Input.Keys.Right)) {


You can subscribe to keyboard events through engine.input.keyboard.on. A KeyEvent object is passed to your handler which offers information about the key that was part of the event.

  • press - When a key was just pressed this frame
  • release - When a key was just released this frame
  • hold - Whenever a key is in the down position
engine.input.keyboard.on("press", (evt: KeyEvent) => {...});
engine.input.keyboard.on("release", (evt: KeyEvent) => {...});
engine.input.keyboard.on("hold", (evt: KeyEvent) => {...});







Private _engine

_engine: Engine

Private _keys

_keys: number[] = []

Private _keysDown

_keysDown: number[] = []

Private _keysUp

_keysUp: number[] = []


eventDispatcher: EventDispatcher

Direct access to the game object event dispatcher.



  • emit(eventName: string, eventObject?: GameEvent): void
  • Emits a new event


    • eventName: string

      Name of the event to emit

    • Optional eventObject: GameEvent

      Data associated with this event

    Returns void


  • Gets list of keys being pressed down

    Returns Keys[]


  • init(global?: any): void
  • Initialize Keyboard event listeners


    • Optional global: any

    Returns void


  • isHeld(key: Keys): boolean
  • Tests if a certain key is held down. This is persisted between frames.


    • key: Keys

      Test whether a key is held down

    Returns boolean


  • off(eventName: string, handler?: function): void
  • Alias for removeEventListener. If only the eventName is specified it will remove all handlers registered for that specific event. If the eventName and the handler instance are specified only that handler will be removed.


    • eventName: string

      Name of the event to listen for

    • Optional handler: function

      Event handler for the thrown event

    Returns void


  • on(eventName: press, handler: function): any
  • on(eventName: release, handler: function): any
  • on(eventName: hold, handler: function): any
  • on(eventName: string, handler: function): any
  • Parameters

    • eventName: press
    • handler: function

    Returns any

  • Parameters

    Returns any

  • Parameters

    • eventName: hold
    • handler: function

    Returns any

  • Parameters

    • eventName: string
    • handler: function

    Returns any


  • update(delta: number): void
  • Parameters

    • delta: number

    Returns void


  • wasPressed(key: Keys): boolean
  • Tests if a certain key was just pressed this frame. This is cleared at the end of the update frame.


    • key: Keys

      Test whether a key was just pressed

    Returns boolean


  • wasReleased(key: Keys): boolean
  • Tests if a certain key was just released this frame. This is cleared at the end of the update frame.


    • key: Keys

      Test whether a key was just released

    Returns boolean

Static extend

  • extend(methods: any): any
  • You may wish to extend native Excalibur functionality in vanilla Javascript. Any method on a class inheriting Class may be extended to support additional functionality. In the example below we create a new type called MyActor.

    var MyActor = Actor.extend({
       constructor: function() {
          this.newprop = 'something';
          Actor.apply(this, arguments);
       update: function(engine, delta) {
          // Implement custom update
          // Call super constructor update
          Actor.prototype.update.call(this, engine, delta);
          console.log("Something cool!");
    var myActor = new MyActor(100, 100, 100, 100, Color.Azure);

    In TypeScript, you only need to use the extends syntax, you do not need to use this method of extension.


    • methods: any

      A JSON object contain any methods/properties you want to extend

    Returns any