Skip to content

Commit 3ec8e8d

Browse files
authored
Merge pull request phaserjs#2920 from vicsparkz/master
Added Feature: JustPressed and JustReleased for keyboard input
2 parents 44c87b1 + b6dcfaa commit 3ec8e8d

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

v2-community/src/input/Key.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

v2-community/src/input/Keyboard.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,32 @@ Phaser.Keyboard.prototype = {
528528

529529
},
530530

531+
justPressed: function (keycode) {
532+
533+
if (this._keys[keycode])
534+
{
535+
return this._keys[keycode].justPressed();
536+
}
537+
else
538+
{
539+
return null;
540+
}
541+
542+
},
543+
544+
justReleased: function (keycode) {
545+
546+
if (this._keys[keycode])
547+
{
548+
return this._keys[keycode].justReleased();
549+
}
550+
else
551+
{
552+
return null;
553+
}
554+
555+
},
556+
531557
/**
532558
* Returns true of the key is currently pressed down. Note that it can only detect key presses on the web browser.
533559
*

0 commit comments

Comments
 (0)