Skip to content

Commit f934066

Browse files
committed
Added Key Events.
1 parent dde0233 commit f934066

12 files changed

Lines changed: 226 additions & 12 deletions

File tree

src/input/keyboard/KeyboardPlugin.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
var Class = require('../../utils/Class');
88
var EventEmitter = require('eventemitter3');
9+
var Events = require('./events');
910
var GetValue = require('../../utils/object/GetValue');
1011
var InputPluginCache = require('../InputPluginCache');
1112
var Key = require('./keys/Key');
@@ -720,16 +721,14 @@ var KeyboardPlugin = new Class({
720721

721722
if (!event.cancelled && (!key || !repeat))
722723
{
723-
// keydown_code event
724724
if (KeyMap[code])
725725
{
726-
this.emit('keydown_' + KeyMap[code], event);
726+
this.emit(Events.KEY_DOWN + KeyMap[code], event);
727727
}
728728

729729
if (!event.cancelled)
730730
{
731-
// keydown event
732-
this.emit(event.type, event);
731+
this.emit(Events.ANY_KEY_DOWN, event);
733732
}
734733
}
735734
}
@@ -743,16 +742,14 @@ var KeyboardPlugin = new Class({
743742

744743
if (!event.cancelled)
745744
{
746-
// keyup_code event
747745
if (KeyMap[code])
748746
{
749-
this.emit('keyup_' + KeyMap[code], event);
747+
this.emit(Events.KEY_UP + KeyMap[code], event);
750748
}
751749

752750
if (!event.cancelled)
753751
{
754-
// keyup event
755-
this.emit(event.type, event);
752+
this.emit(Events.ANY_KEY_UP, event);
756753
}
757754
}
758755
}

src/input/keyboard/combo/KeyCombo.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
var Class = require('../../../utils/Class');
8+
var Events = require('../events');
89
var GetFastValue = require('../../../utils/object/GetFastValue');
910
var ProcessKeyCombo = require('./ProcessKeyCombo');
1011
var ResetKeyCombo = require('./ResetKeyCombo');
@@ -235,7 +236,7 @@ var KeyCombo = new Class({
235236

236237
if (matched)
237238
{
238-
_this.manager.emit('keycombomatch', _this, event);
239+
_this.manager.emit(Events.COMBO_MATCH, _this, event);
239240

240241
if (_this.resetOnMatch)
241242
{
@@ -254,6 +255,7 @@ var KeyCombo = new Class({
254255
* @name Phaser.Input.Keyboard.KeyCombo#onKeyDown
255256
* @private
256257
* @type {KeyboardKeydownCallback}
258+
* @fires Phaser.Input.Keyboard.Events#COMBO_MATCH
257259
* @since 3.0.0
258260
*/
259261
this.onKeyDown = onKeyDownHandler;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Global Key Down Event.
9+
*
10+
* This event is dispatched by the Keyboard Plugin when any key on the keyboard is pressed down.
11+
*
12+
* Listen to this event from within a Scene using: `this.input.keyboard.on('keydown', listener)`.
13+
*
14+
* You can also listen for a specific key being pressed. See [Keyboard.Events.KEY_DOWN]{Phaser.Input.Keyboard.Events#KEY_DOWN} for details.
15+
*
16+
* Finally, you can create Key objects, which you can also listen for events from. See [Keyboard.Events.DOWN]{Phaser.Input.Keyboard.Events#DOWN} for details.
17+
*
18+
* _Note_: Many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
19+
* See http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/ for more details.
20+
*
21+
* Also, please be aware that some browser extensions can disable or override Phaser keyboard handling.
22+
* For example, the Chrome extension vimium is known to disable Phaser from using the D key, while EverNote disables the backtick key.
23+
* There are others. So, please check your extensions if you find you have specific keys that don't work.
24+
*
25+
* @event Phaser.Input.Keyboard.Events#ANY_KEY_DOWN
26+
*
27+
* @param {KeyboardEvent} event - The native DOM Keyboard Event. You can inspect this to learn more about the key that was pressed, any modifiers, etc.
28+
*/
29+
module.exports = 'keydown';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Global Key Up Event.
9+
*
10+
* This event is dispatched by the Keyboard Plugin when any key on the keyboard is released.
11+
*
12+
* Listen to this event from within a Scene using: `this.input.keyboard.on('keyup', listener)`.
13+
*
14+
* You can also listen for a specific key being released. See [Keyboard.Events.KEY_UP]{Phaser.Input.Keyboard.Events#KEY_UP} for details.
15+
*
16+
* Finally, you can create Key objects, which you can also listen for events from. See [Keyboard.Events.UP]{Phaser.Input.Keyboard.Events#UP} for details.
17+
*
18+
* @event Phaser.Input.Keyboard.Events#ANY_KEY_UP
19+
*
20+
* @param {KeyboardEvent} event - The native DOM Keyboard Event. You can inspect this to learn more about the key that was released, any modifiers, etc.
21+
*/
22+
module.exports = 'keyup';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Key Combo Match Event.
9+
*
10+
* This event is dispatched by the Keyboard Plugin when a [Key Combo]{Phaser.Input.Keyboard.KeyCombo} is matched.
11+
*
12+
* Listen for this event from the Key Plugin after a combo has been created:
13+
*
14+
* ```javascript
15+
* this.input.keyboard.createCombo([ 38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13 ], { resetOnMatch: true });
16+
*
17+
* this.input.keyboard.on('keycombomatch', function (event) {
18+
* console.log('Konami Code entered!');
19+
* });
20+
* ```
21+
*
22+
* @event Phaser.Input.Keyboard.Events#COMBO_MATCH
23+
*
24+
* @param {Phaser.Input.Keyboard.KeyCombo} keycombo - The Key Combo object that was matched.
25+
* @param {KeyboardEvent} event - The native DOM Keyboard Event of the final key in the combo. You can inspect this to learn more about any modifiers, etc.
26+
*/
27+
module.exports = 'keycombomatch';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Key Down Event.
9+
*
10+
* This event is dispatched by a [Key]{Phaser.Input.Keyboard.Key} object when it is pressed.
11+
*
12+
* Listen for this event from the Key object instance directly:
13+
*
14+
* ```javascript
15+
* var spaceBar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
16+
*
17+
* spaceBar.on('down', listener)
18+
* ```
19+
*
20+
* You can also create a generic 'global' listener. See [Keyboard.Events.ANY_KEY_DOWN]{Phaser.Input.Keyboard.Events#ANY_KEY_DOWN} for details.
21+
*
22+
* @event Phaser.Input.Keyboard.Events#DOWN
23+
*
24+
* @param {Phaser.Input.Keyboard.Key} key - The Key object that was pressed.
25+
* @param {KeyboardEvent} event - The native DOM Keyboard Event. You can inspect this to learn more about any modifiers, etc.
26+
*/
27+
module.exports = 'down';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Key Down Event.
9+
*
10+
* This event is dispatched by the Keyboard Plugin when any key on the keyboard is pressed down.
11+
*
12+
* Unlike the `ANY_KEY_DOWN` event, this one has a special dynamic event name. For example, to listen for the `A` key being pressed
13+
* use the following from within a Scene: `this.input.keyboard.on('keydown-A', listener)`. You can replace the `-A` part of the event
14+
* name with any valid [Key Code string]{Phaser.Input.Keyboard.KeyCodes}. For example, this will listen for the space bar:
15+
* `this.input.keyboard.on('keydown-SPACE', listener)`.
16+
*
17+
* You can also create a generic 'global' listener. See [Keyboard.Events.ANY_KEY_DOWN]{Phaser.Input.Keyboard.Events#ANY_KEY_DOWN} for details.
18+
*
19+
* Finally, you can create Key objects, which you can also listen for events from. See [Keyboard.Events.DOWN]{Phaser.Input.Keyboard.Events#DOWN} for details.
20+
*
21+
* _Note_: Many keyboards are unable to process certain combinations of keys due to hardware limitations known as ghosting.
22+
* See http://www.html5gamedevs.com/topic/4876-impossible-to-use-more-than-2-keyboard-input-buttons-at-the-same-time/ for more details.
23+
*
24+
* Also, please be aware that some browser extensions can disable or override Phaser keyboard handling.
25+
* For example, the Chrome extension vimium is known to disable Phaser from using the D key, while EverNote disables the backtick key.
26+
* There are others. So, please check your extensions if you find you have specific keys that don't work.
27+
*
28+
* @event Phaser.Input.Keyboard.Events#KEY_DOWN
29+
*
30+
* @param {KeyboardEvent} event - The native DOM Keyboard Event. You can inspect this to learn more about the key that was pressed, any modifiers, etc.
31+
*/
32+
module.exports = 'keydown-';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Key Up Event.
9+
*
10+
* This event is dispatched by the Keyboard Plugin when any key on the keyboard is released.
11+
*
12+
* Unlike the `ANY_KEY_UP` event, this one has a special dynamic event name. For example, to listen for the `A` key being released
13+
* use the following from within a Scene: `this.input.keyboard.on('keyup-A', listener)`. You can replace the `-A` part of the event
14+
* name with any valid [Key Code string]{Phaser.Input.Keyboard.KeyCodes}. For example, this will listen for the space bar:
15+
* `this.input.keyboard.on('keyup-SPACE', listener)`.
16+
*
17+
* You can also create a generic 'global' listener. See [Keyboard.Events.ANY_KEY_UP]{Phaser.Input.Keyboard.Events#ANY_KEY_UP} for details.
18+
*
19+
* Finally, you can create Key objects, which you can also listen for events from. See [Keyboard.Events.UP]{Phaser.Input.Keyboard.Events#UP} for details.
20+
*
21+
* @event Phaser.Input.Keyboard.Events#KEY_UP
22+
*
23+
* @param {KeyboardEvent} event - The native DOM Keyboard Event. You can inspect this to learn more about the key that was released, any modifiers, etc.
24+
*/
25+
module.exports = 'keyup-';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The Key Up Event.
9+
*
10+
* This event is dispatched by a [Key]{Phaser.Input.Keyboard.Key} object when it is released.
11+
*
12+
* Listen for this event from the Key object instance directly:
13+
*
14+
* ```javascript
15+
* var spaceBar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
16+
*
17+
* spaceBar.on('up', listener)
18+
* ```
19+
*
20+
* You can also create a generic 'global' listener. See [Keyboard.Events.ANY_KEY_UP]{Phaser.Input.Keyboard.Events#ANY_KEY_UP} for details.
21+
*
22+
* @event Phaser.Input.Keyboard.Events#UP
23+
*
24+
* @param {Phaser.Input.Keyboard.Key} key - The Key object that was released.
25+
* @param {KeyboardEvent} event - The native DOM Keyboard Event. You can inspect this to learn more about any modifiers, etc.
26+
*/
27+
module.exports = 'up';

src/input/keyboard/events/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2019 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* @namespace Phaser.Input.Keyboard.Events
9+
*/
10+
11+
module.exports = {
12+
13+
ANY_KEY_DOWN: require('./ANY_KEY_DOWN_EVENT'),
14+
ANY_KEY_UP: require('./ANY_KEY_UP_EVENT'),
15+
COMBO_MATCH: require('./COMBO_MATCH_EVENT'),
16+
DOWN: require('./DOWN_EVENT'),
17+
KEY_DOWN: require('./KEY_DOWN_EVENT'),
18+
KEY_UP: require('./KEY_UP_EVENT'),
19+
UP: require('./UP_EVENT')
20+
21+
};

0 commit comments

Comments
 (0)