Key Down Duration
Demonstrates how to use the getDuration method to track exactly how long a specific keyboard key has been held down in milliseconds.
800 × 600
class Example extends Phaser.Scene
{
text;
key;
create ()
{
this.key = this.input.keyboard.addKey('A');
this.add.text(10, 10, 'Hold down the A Key', { font: '16px Courier', fill: '#00ff00' });
this.text = this.add.text(10, 30, '', { font: '16px Courier', fill: '#00ff00' });
}
update ()
{
this.text.setText(`Duration: ${this.key.getDuration()}ms`);
}
}
const config = {
scale: { mode: Phaser.Scale.FIT },
type: Phaser.AUTO,
parent: 'game-container',
width: 800,
height: 600,
scene: Example
};
const game = new Phaser.Game(config);