KeyboardSystem System

Provides access to key events.

Note: Events and methods are inherited from the KeyboardState component.

The event callbacks are triggered with a native KeyboardEvent received by window.document, which is wrapped in a standard Crafty event object (as described in KeyboardState).

These key events are triggered globally, thus on the global Crafty instance, every entity and system.

Example

Move viewport by arrow keys.

Crafty.bind('KeyDown', function(e) {
    if (e.key === Crafty.keys.LEFT_ARROW) {
        Crafty.viewport.x++;
    } else if (e.key === Crafty.keys.RIGHT_ARROW) {
        Crafty.viewport.x--;
    } else if (e.key === Crafty.keys.UP_ARROW) {
        Crafty.viewport.y++;
    } else if (e.key === Crafty.keys.DOWN_ARROW) {
        Crafty.viewport.y--;
    }
});