@@ -81,6 +81,14 @@ Phaser.Key = function (game, keycode) {
8181 */
8282 this . timeUp = - 2500 ;
8383
84+ /**
85+ * If the key is up this value holds the duration of that key release and is constantly updated.
86+ * If the key is down it holds the duration of the previous up session.
87+ * @property {number } duration - The number of milliseconds this key has been up for.
88+ * @default
89+ */
90+ this . durationUp = - 2500 ;
91+
8492 /**
8593 * @property {number } repeats - If a key is held down this holds down the number of times the key has 'repeated'.
8694 * @default
@@ -148,6 +156,10 @@ Phaser.Key.prototype = {
148156 this . onHoldCallback . call ( this . onHoldContext , this ) ;
149157 }
150158 }
159+ else
160+ {
161+ this . durationUp = this . game . time . time - this . timeUp ;
162+ }
151163
152164 } ,
153165
@@ -178,6 +190,7 @@ Phaser.Key.prototype = {
178190 this . isUp = false ;
179191 this . timeDown = this . game . time . time ;
180192 this . duration = 0 ;
193+ this . durationUp = this . game . time . time - this . timeUp ;
181194 this . repeats = 0 ;
182195
183196 // _justDown will remain true until it is read via the justDown Getter
@@ -210,6 +223,7 @@ Phaser.Key.prototype = {
210223 this . isUp = true ;
211224 this . timeUp = this . game . time . time ;
212225 this . duration = this . game . time . time - this . timeDown ;
226+ this . durationUp = 0 ;
213227
214228 // _justUp will remain true until it is read via the justUp Getter
215229 // this enables the game to poll for past presses, or reset it at the start of a new game state
@@ -236,6 +250,7 @@ Phaser.Key.prototype = {
236250 this . isUp = true ;
237251 this . timeUp = this . game . time . time ;
238252 this . duration = 0 ;
253+ this . durationUp = - 2500 ;
239254 this . _enabled = true ; // .enabled causes reset(false)
240255 this . _justDown = false ;
241256 this . _justUp = false ;
@@ -280,6 +295,32 @@ Phaser.Key.prototype = {
280295
281296 return ( ! this . isDown && ( ( this . game . time . time - this . timeUp ) < duration ) ) ;
282297
298+ } ,
299+
300+ /**
301+ * Returns `true` if the Key was just pressed down this update tick, or `false` if it either isn't down,
302+ * or was pressed down on a previous update tick.
303+ *
304+ * @method Phaser.Key#justPressed
305+ * @return {boolean } True if the key was just pressed down this update tick.
306+ */
307+ justPressed : function ( ) {
308+
309+ return ( this . isDown && this . duration === 0 ) ;
310+
311+ } ,
312+
313+ /**
314+ * Returns `true` if the Key was just released this update tick, or `false` if it either isn't up,
315+ * or was released on a previous update tick.
316+ *
317+ * @method Phaser.Key#justReleased
318+ * @return {boolean } True if the key was just released this update tick.
319+ */
320+ justReleased : function ( ) {
321+
322+ return ( ! this . isDown && this . durationUp === 0 ) ;
323+
283324 }
284325
285326} ;
0 commit comments