Keyboard Component

Provides the entity with keyboard events. Keyboard events get dispatched to all entities that have the Keyboard component.

Note: If you do not add this component, key events will not be triggered on the entity.

Triggers all events described in the KeyboardState component, these are:

Events

KeyDown [Data = {KeyboardEvent}]
when a key is pressed
KeyUp [Data = {KeyboardEvent}]
when a key is released

Example

Crafty.e("2D, DOM, Color, Keyboard")
  .attr({x: 100, y: 100, w: 50, h: 50})
  .color("red")
  .bind('KeyDown', function(e) {
    if (e.key == Crafty.keys.LEFT_ARROW) {
      this.x -= 1;
    } else if (e.key == Crafty.keys.RIGHT_ARROW) {
      this.x += 1;
    } else if (e.key == Crafty.keys.UP_ARROW) {
      this.y -= 1;
    } else if (e.key == Crafty.keys.DOWN_ARROW) {
      this.y += 1;
    }
  });