55 */
66
77var Class = require ( '../../../utils/Class' ) ;
8+ var EventEmitter = require ( 'eventemitter3' ) ;
89
910/**
1011 * @classdesc
1112 * A generic Key object which can be passed to the Process functions (and so on)
1213 * keycode must be an integer
1314 *
1415 * @class Key
16+ * @extends Phaser.Events.EventEmitter
1517 * @memberof Phaser.Input.Keyboard
1618 * @constructor
1719 * @since 3.0.0
@@ -20,10 +22,14 @@ var Class = require('../../../utils/Class');
2022 */
2123var Key = new Class ( {
2224
25+ Extends : EventEmitter ,
26+
2327 initialize :
2428
2529 function Key ( keyCode )
2630 {
31+ EventEmitter . call ( this ) ;
32+
2733 /**
2834 * The keycode of this key.
2935 *
@@ -42,16 +48,6 @@ var Key = new Class({
4248 */
4349 this . originalEvent = undefined ;
4450
45- /**
46- * Should this Key prevent event propagation?
47- *
48- * @name Phaser.Input.Keyboard.Key#preventDefault
49- * @type {boolean }
50- * @default true
51- * @since 3.0.0
52- */
53- this . preventDefault = true ;
54-
5551 /**
5652 * Can this Key be processed?
5753 *
@@ -163,6 +159,19 @@ var Key = new Class({
163159 */
164160 this . timeUp = 0 ;
165161
162+ /**
163+ * When a key is held down should it continuously fire the `down` event each time it repeats?
164+ *
165+ * By default it will emit the `down` event just once, but if you wish to receive the event
166+ * for each repeat as well, enable this property.
167+ *
168+ * @name Phaser.Input.Keyboard.Key#emitOnRepeat
169+ * @type {boolean }
170+ * @default false
171+ * @since 3.16.0
172+ */
173+ this . emitOnRepeat = false ;
174+
166175 /**
167176 * If a key is held down this holds down the number of times the key has 'repeated'.
168177 *
@@ -206,10 +215,102 @@ var Key = new Class({
206215 this . _tick = - 1 ;
207216 } ,
208217
218+ /**
219+ * Controls if this Key will continuously emit a `down` event while being held down (true),
220+ * or emit the event just once, on first press, and then skip future events (false).
221+ *
222+ * @method Phaser.Input.Keyboard.Key#setEmitOnRepeat
223+ * @since 3.16.0
224+ *
225+ * @param {boolean } value - Emit `down` events on repeated key down actions, or just once?
226+ *
227+ * @return {Phaser.Input.Keyboard.Key } This Key instance.
228+ */
229+ setEmitOnRepeat : function ( value )
230+ {
231+ this . emitOnRepeat = value ;
232+
233+ return this ;
234+ } ,
235+
236+ /**
237+ * Processes the Key Down action for this Key.
238+ * Called automatically by the Keyboard Plugin.
239+ *
240+ * @method Phaser.Input.Keyboard.Key#onDown
241+ * @since 3.16.0
242+ *
243+ * @param {KeyboardEvent } event - The native DOM Keyboard event.
244+ */
245+ onDown : function ( event )
246+ {
247+ this . originalEvent = event ;
248+
249+ if ( ! this . enabled )
250+ {
251+ return ;
252+ }
253+
254+ this . altKey = event . altKey ;
255+ this . ctrlKey = event . ctrlKey ;
256+ this . shiftKey = event . shiftKey ;
257+ this . metaKey = event . metaKey ;
258+ this . location = event . location ;
259+
260+ this . repeats ++ ;
261+
262+ if ( ! this . isDown )
263+ {
264+ this . isDown = true ;
265+ this . isUp = false ;
266+ this . timeDown = event . timeStamp ;
267+ this . duration = 0 ;
268+ this . _justDown = true ;
269+ this . _justUp = false ;
270+
271+ this . emit ( 'down' , this , event ) ;
272+ }
273+ else if ( this . emitOnRepeat )
274+ {
275+ this . emit ( 'down' , this , event ) ;
276+ }
277+ } ,
278+
279+ /**
280+ * Processes the Key Up action for this Key.
281+ * Called automatically by the Keyboard Plugin.
282+ *
283+ * @method Phaser.Input.Keyboard.Key#onUp
284+ * @since 3.16.0
285+ *
286+ * @param {KeyboardEvent } event - The native DOM Keyboard event.
287+ */
288+ onUp : function ( event )
289+ {
290+ this . originalEvent = event ;
291+
292+ if ( ! this . enabled )
293+ {
294+ return ;
295+ }
296+
297+ this . isDown = false ;
298+ this . isUp = true ;
299+ this . timeUp = event . timeStamp ;
300+ this . duration = this . timeUp - this . timeDown ;
301+ this . repeats = 0 ;
302+
303+ this . _justDown = false ;
304+ this . _justUp = true ;
305+ this . _tick = - 1 ;
306+
307+ this . emit ( 'up' , this , event ) ;
308+ } ,
309+
209310 /**
210311 * Resets this Key object back to its default un-pressed state.
211312 *
212- * @method Phaser.Input.Keyboard.Key. reset
313+ * @method Phaser.Input.Keyboard.Key# reset
213314 * @since 3.6.0
214315 *
215316 * @return {Phaser.Input.Keyboard.Key } This Key instance.
@@ -233,6 +334,19 @@ var Key = new Class({
233334 this . _tick = - 1 ;
234335
235336 return this ;
337+ } ,
338+
339+ /**
340+ * Removes any bound event handlers and removes local references.
341+ *
342+ * @method Phaser.Input.Keyboard.Key#destroy
343+ * @since 3.16.0
344+ */
345+ destroy : function ( )
346+ {
347+ this . removeAllListeners ( ) ;
348+
349+ this . originalEvent = null ;
236350 }
237351
238352} ) ;
0 commit comments