[SOLVED] keyboard.wasPressed not working

Hi all,

I am having a problem - keyboard.wasPressed is running the same way as keyboard.isPressed. Could you please tell me how to fix my code?

Rgds,

var CodeJjkk = pc.createScript('codeJjkk');

// initialize code called once per entity
CodeJjkk.prototype.initialize = function() {
    
};

// update code called every frame
CodeJjkk.prototype.update = function(dt) {
    //declaration
    
    var codpos = 1;
    var count = 1;
    var VarOfGreatness = this.app.keyboard.wasPressed(pc.KEY_1);
    //logic
    for (count = 0; count < 10; count ++){
        if (VarOfGreatness){
            if (codpos === 1 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward1');
                fd.setLocalPosition(-1.106, 1.842, -5.225);
                codpos ++;
            }
            else if (codpos === 2 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward2');
                fd.setLocalPosition(-0.57, 1.84, -5.225);
                codpos ++;
            }
            else if (codpos === 3 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward3');
                fd.setLocalPosition(-0.071, 1.84, -5.225);
                codpos ++;
            }
            else if (codpos === 4 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward4');
                fd.setLocalPosition(0.429, 1.84, -5.225);
                codpos ++;
            }
            else if (codpos === 5 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward5');
                fd.setLocalPosition(0.95, 1.84, -5.225);
                codpos ++;
            }
            else if (codpos === 6 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward6');
                fd.setLocalPosition(-1.1,  1.392, -5.225);
                codpos ++;
            }
            else if (codpos === 7 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward7');
                fd.setLocalPosition(-0.608, -1.392, -5.225);
                codpos ++;
            }
            else if (codpos === 8 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward8');
                fd.setLocalPosition(0.02, 1.392, -5.225);
                codpos ++;
            }
            else if (codpos === 9 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward9');
                fd.setLocalPosition(0.525, 1.392, -5.225);
                codpos ++;
            }
            else if (codpos === 10 && this.app.keyboard.wasReleased === 0){
                fd = this.app.root.findByName('Forward10');
                fd.setLocalPosition(1.007, 1.392, -5.225);
                codpos ++;
            }
        }
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// CodeJjkk.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

You have several issues that I can see here but they are not to do with with the keyboard API.

if (codpos === 1 && this.app.keyboard.wasReleased === 0){ will never evaluate to true as this.app.keyboard.wasReleased is a function that is meant to used in a similar way to wasPressed. ie: wasReleased(pc.KEY_1").

Even then, it is impossible for the same key to be pressed and released in the same frame which is what it feels like you are trying to do here.


for (count = 0; count < 10; count ++){
        if (VarOfGreatness){

The other issue is that you are looping through and moving all the entities in a single frame when the key is pressed. You are effective saying, “If the key is pressed, loop through all the entities and move them”. The question here is why do you have a for loop?

I have a for loop because I have to move 10 entities sequentially to their respective positions; i.e, each time the key 1 is pressed, then move the next entity in line to it’s respective position.

How will it be looping through all the entities without the loop? I need to detect the key press multiple times, which is why I have a for loop.

It’s not working without the wasReleased either

Fixed (slimmed down version of your code) https://playcanvas.com/editor/scene/622171

Press Space bar to move the letters sequentially.

You were almost there but using a loop made it do it all in a single frame (you can see this if you stepped through with the debugger) as you were incrementing the index you were using on each iteration of the loop.

So you were literally going:
first iteration, move Forward1, increment codpos
second iteration, move Forward2, increment codpos
etc

All in the same loop and the same frame when you wanted to only advance one.

Here is another variant this is closer to your original code: https://playcanvas.com/editor/code/563287?tabs=13156536

Thanks a lot for helping me out! I would really appreciate it if you could tell me how to use the debugger.

Thanks again,

@coderMACi5

https://developer.playcanvas.com/en/user-manual/scripting/debugging/

Thanks a lot for the third time :grinning: