Keyboard events
Introduction¶
Built-in keyboard events of phaser.
- Author: Richard Davey
Usage¶
Quick start¶
- Is key-down/is key-up
var keyObj = scene.input.keyboard.addKey('W'); // Get key object var isDown = keyObj.isDown; var isUp = keyObj.isUp;
- Key is down after a duration
var keyObj = scene.input.keyboard.addKey('W'); // Get key object var isDown = scene.input.keyboard.checkDown(keyObj, duration);
- On key-down/on key-up
or
var keyObj = scene.input.keyboard.addKey('W'); // Get key object keyObj.on('down', function(event) { /* ... */ }); keyObj.on('up', function(event) { /* ... */ });
scene.input.keyboard.on('keydown-' + 'W', function (event) { /* ... */ }); scene.input.keyboard.on('keyup-' + 'W', function (event) { /* ... */ });
- Any key-down/any key-up
scene.input.keyboard.on('keydown', function (event) { /* ... */ }); scene.input.keyboard.on('keyup', function (event) { /* ... */ });
event
: KeyboardEventevent.code
: 'Key' + 'W'
Key object¶
- Get key object
or
var keyObj = scene.input.keyboard.addKey('W'); // see `Key map` section // var keyObj = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
var keyObj = scene.input.keyboard.addKey('W', enableCapture, emitOnRepeat);
enableCapture
: Automatically callpreventDefault
on the native DOM browser event for the key codes being added.emitOnRepeat
: Controls if the Key will continuously emit a 'down' event while being held down (true), or emit the event just once (false, the default).
- Get key objects
or
var keys = scene.input.keyboard.addKeys('W,S,A,D'); // keys.W, keys.S, keys.A, keys.D // var keys = scene.input.keyboard.addKeys('W,S,A,D', enableCapture, emitOnRepeat);
var keys = scene.input.keyboard.addKeys({ up: 'up', down: 'down', left: 'left', right: 'right' }); // keys.up, keys.down, keys.left, keys.right
- Remove key object
scene.input.keyboard.removeKey('W'); // scene.input.keyboard.removeKey(Phaser.Input.Keyboard.KeyCodes.W); // scene.input.keyboard.removeKey(key, destroy, removeCapture);
destroy
: CallKey.destroy
on each removed Key objectremoveCapture
: Remove all key captures for Key objects owened by this plugin?
- Remove all key objects
scene.input.keyboard.removeAllKeys(true); // scene.input.keyboard.removeAllKeys(destroy, removeCapture);
destroy
: CallKey.destroy
on each removed Key objectremoveCapture
: Remove all key captures for Key objects owened by this plugin?
- Key-down/key-up state
var isDown = keyObj.isDown; var isUp = keyObj.isUp;
- Duration of key-down
var duration = keyObj.getDuration(); // ms
- Enable/disable
keyObj.enabled = enabled; // Set false to disable key event
Key object of cursorkeys¶
- Get key state object
var cursorKeys = scene.input.keyboard.createCursorKeys();
- Get key state
var isUpDown = cursorKeys.up.isDown; var isDownDown = cursorKeys.down.isDown; var isLeftDown = cursorKeys.left.isDown; var isRightDown = cursorKeys.right.isDown; var isSpaceDown = cursorKeys.space.isDown; var isShiftDown = cursorKeys.shift.isDown;
Order of key-down/key-up events¶
- Key-down/key-up events of key object
var keyObj = scene.input.keyboard.addKey('W'); // Get key object keyObj.on('down', function(event) { /* ... */ }); keyObj.on('up', function(event) { /* ... */ });
event.stopImmediatePropagation()
: Stop any further listeners from being invoked in the current Scene.event.stopPropagation()
: Stop it reaching any other Scene.
- On key-down/on key-up
scene.input.keyboard.on('keydown-' + 'W', function (event) { /* ... */ }); scene.input.keyboard.on('keyup-' + 'W', function (event) { /* ... */ });
event.stopImmediatePropagation()
: Stop any further listeners from being invoked in the current Scene.event.stopPropagation()
: Stop it reaching any other Scene.
- Any key-down/on key-up
scene.input.keyboard.on('keydown', function (event) { /* ... */ }); scene.input.keyboard.on('keyup', function (event) { /* ... */ });
event.key
:'a'
event.keyCode
:65
event.code
:'KeyA'
event.stopImmediatePropagation()
: Stop any further listeners from being invoked in the current Scene.event.stopPropagation()
: Stop it reaching any other Scene.
Destroy key object¶
keyObj.destroy();
Key map¶
A
~Z
F1
~F12
BACKSPACE
TAB
ENTER
SHIFT
CTRL
.ALT
PAUSE
CAPS_LOCK
ESC
SPACE
PAGE_UP
,PAGE_DOWN
END
,HOME
LEFT
,UP
,RIGHT
,DOWN
PRINT_SCREEN
INSERT
,DELETE
ZERO
,ONE
,TWO
,THREE
,FOUR
,FIVE
,SIX
,SEVEN
,EIGHT
,NINE
NUMPAD_ZERO
,NUMPAD_ONE
,NUMPAD_TWO
,NUMPAD_THREE
,NUMPAD_FOUR
,NUMPAD_FIVE
,NUMPAD_SIX
,NUMPAD_SEVEN
,NUMPAD_EIGHT
,NUMPAD_NINE
,NUMPAD_ADD
,NUMPAD_SUBTRACT
OPEN_BRACKET
,CLOSED_BRACKET
SEMICOLON_FIREFOX
,COLON
,COMMA_FIREFOX_WINDOWS
,COMMA_FIREFOX
,BRACKET_RIGHT_FIREFOX
,BRACKET_LEFT_FIREFOX