[SOLVED] How to detect if key was pressed in certain time frame

hello so i want to find a way to detect if a key is pressed in a certain time frame ex: if i press space then if i press W in the following lets say 5 seconds something will happen. maybe i could do this with timers but m not sure how

if(this.app.keyboard.wasPressed(pc.KEY_W) && this.app.keyboard.waspressed(pc.KEY_SPACE)){
   setTimeout(function(){
       alert("you waited 5 seconds which is 5000 milliseconds");
   }.bind(this),5000);
}

ok so then do i just add my other part after bind or what

ok so im not sure how this works exactly but do you have it so if w and space are pressed at the same time it sets a timer right if that is the case tthis wont work becuase i need it to check after youve pressed one key then if you press the other key in the time it sets if it is the way i just said then this should work

if you were to add your other part after the function it would play before the wait

ok so before the bind then

this will detect if a key is pressed after the other key though right not the same time

well then just put the if statement inside the function.

ok but you didnt answer my question this will check if a key is pressed after another key right?!

yep if you do it like this :slight_smile::

if(this.app.keyboard.isPressed(pc.KEY_W)){
    setTimeout(function(){
       if(this.app.keyboard.isPressed(pc.KEY_SPACE)){
           // do whatever here
       }
    }.bind(this),5000);
}

ok thanks dude

1 Like

np

so i tried this and it didnt work not sure why though
if (this.app.keyboard.isPressed(pc.KEY_DOWN) && this.app.keyboard.isPressed(pc.KEY_LEFT)) {
this.entity.rigidbody.applyForce(-135, 0, 0);
}
if(this.app.keyboard.isPressed(pc.KEY_W)){
setTimeout(function(){
if(this.app.keyboard.isPressed(pc.KEY_SPACE)){
this.entity.rigidbody.applyForce(-135, 0, 0);
}
}.bind(this),5000);
}

};

, if (this.app.keyboard.isPressed(pc.KEY_DOWN) && this.app.keyboard.isPressed(pc.KEY_LEFT)) {
this.entity.rigidbody.applyForce(-135, 0, 0);
}
if(this.app.keyboard.isPressed(pc.KEY_W)){
setTimeout(function(){
if(this.app.keyboard.isPressed(pc.KEY_SPACE)){
this.entity.rigidbody.applyForce(-135, 0, 0);
}
}.bind(this),5000);
}

};

it wont let me put the code but you get the idea

try if(this.app.keyboard.wasPressed(pc.KEY_W)).

i did

works fine for me.

wierd ill try again

here send me a link to wherever your testing this