Crafty.keyboardDispatch()
Internal method which dispatches keyboard events received by Crafty.
Events
- KeyDown [Crafty's KeyboardEvent = { key: `Crafty.keys` keyCode (Number), originalEvent: original KeyboardEvent }]
- is triggered for each entity when the DOM 'keydown' event is triggered.
- KeyUp [Crafty's KeyboardEvent = { key: `Crafty.keys` keyCode (Number), originalEvent: original KeyboardEvent }]
- is triggered for each entity when the DOM 'keyup' event is triggered.
This method processes a native KeyboardEvent
received by window.document
,
wraps it in a custom event object (for cross-browser compatibility) and dispatches it to the global Crafty object and thus to every entity.
This method also updates Crafty.keydown
.
Example
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--;
}
});