Controls System

A built-in system for linking specific inputs to general types of input events.

Note: The methods provided by this system are likely to change in future verisons of Crafty, as more input types are supported.


TriggerInputDown [Data = {name}]
When a trigger group is activated
TriggerInputUp [Data = {name, downFor}]
When a trigger group is released
DirectionalInput [Data = {name, x, y}]
When a directional input changes
ControlDefined [Data = {type, name}]
When a control input is defined
ControlDestroyed [Data = {type, name}]
When a control input is destroyed


Back to top


defineDpad(string name, obj definition[, obj options])

a name for the dpad input


an object which defines the inputs and directions for the dpad


a set of options for the dpad

A dpad is a type of directional control which maps a set of triggers to a set of directions.

The options object has two properties:

  • normalize (bool): If true, the directional input will be normalized to a unit vector. Defaults to false.
  • multipleDirectionBehavior (string): How to behave when multiple directions are active at the same time. Values are "first", "last", and "all". Defaults to "all".


// Define a two-direction dpad, with two keys each bound to the right and left directions
Crafty.s("Controls").defineDpad("MyDpad", {RIGHT_ARROW: 0, LEFT_ARROW: 180, D: 0, A: 180});
Back to top


defineTriggerGroup(string name, obj definition)

a name for the trigger group


an object which defines the inputs for the trigger

A trigger group is a set of togglable inputs mapped to the same event. If any of the inputs are down, the trigger is considered down. If all are up, it is considered up. When the trigger state changes, a TriggerInputUp or TriggerInputDown event is fired.

The definition object lists the inputs that are mapped to the trigger:

  • keys: An array of Crafty keycodes
  • mouseButtons: An array of Crafty mouse button codes


// Define a trigger group mapped to the left mouse button and the A and B keys.
Crafty.s("Controls").defineTriggerGroup("MyTrigger", {
  mouseButtons: [Crafty.mouseButtons.LEFT],
  keys: [Crafty.keys.A, Crafty.keys.B]
Back to top


destroyDpad(string name)

the name of the dpad input

Destroys a previously defined dpad.

See Also

Back to top


destroyTriggerGroup(string name)

the name of the trigger group

Destroys a previously defined trigger group.