How to detect a double press of a key

I have no Idea where to even start on this one I really need help.

With a timer. Start the timer with the first key press and check if there is a second key press within a small time range. Don’t forget to reset the timer.

Hey @Albertos can you give some example code?

var Timer = pc.createScript('timer');

// initialize code called once per entity
Timer.prototype.initialize = function() {
    this.timer = 0;
    this.timerActive = false;
};

// update code called every frame
Timer.prototype.update = function(dt) {
    if (this.timerActive) {
        this.timer += dt;

        if (this.timer >= 0.5) {
            console.log("Single Press");
            this.timerActive = false;
            this.timer = 0;
        }
    } 

    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if (this.timerActive && this.timer < 0.5) {
            console.log("Double press");
            this.timerActive = false;
            this.timer = 0;
        }
        else {
            this.timerActive = true;
            this.timer = 0;
        }
    }
};

Hey @Albertos It didn’t work I’m trying to make it so that single press then hold is walk and double tap then hold is sprint

Here Is My Code If You Want To Have A Look
https://playcanvas.com/editor/code/860529?tabs=62890522,62893755,62893392

You can’t say this. I have tested it and it does exactly what you ask for.

Where do I read this information in your first post?

Yeah that’s my bad I forgot to include it

also if I double press it sends my player entity to the next dimension

First start to figure out how my code is working. Then try to expand (or change) the code to make it work with your own code.

ok

var Timer = pc.createScript('timer');

// initialize code called once per entity
Timer.prototype.initialize = function() {
    this.timer = 0;
    this.timerActive = false;

    this.singlePress = false;
    this.doublePress = false;
};

// update code called every frame
Timer.prototype.update = function(dt) {

    // Single or double press
    if (this.timerActive) {
        this.timer += dt;

        if (this.timer >= 0.5) {
            console.log("Single Press");
            this.singlePress = true;
            this.timerActive = false;
            this.timer = 0;
        }
    } 

    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if (this.timerActive && this.timer < 0.5) {
            console.log("Double Press");
            this.doublePress = true;
            this.timerActive = false;
            this.timer = 0;
        }
        else {
            this.timerActive = true;
        }
    }

    // Walk or sprint
    if (this.app.keyboard.isPressed(pc.KEY_SPACE)) {
        if (this.singlePress) {
            console.log("Walk");
        }
        else if (this.doublePress) {
            console.log("Sprint");
        }
    }
    else {
        this.singlePress = false;
        this.doublePress = false;
    }
};

Alright I’m Just Going To Scrap Sprinting For This Game But I May Come Back To It In The Future