[SOLVED] keyboard.wasPressed keeps running the same way as keyboard.isPressed

Here’s a fiddle:
https://jsfiddle.net/shanadeshana/uabec3fm/2/

If you press the left key, the cube will keep rotating instead of applying the rotation just once

But the documentation says that wasPressed should only work once, unlike isPressed:
https://developer.playcanvas.com/en/tutorials/keyboard-input/

IS this a bug? or am I doing something wrong?

I’ve fixed your code for you:

// Create a PlayCanvas application
var canvas = document.getElementById("render");
var app = new pc.Application(canvas, {
    keyboard: new pc.Keyboard(window)
});
app.start();

// Fill the available space at full resolution
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);


// Create box entity
var cube = new pc.Entity();
cube.addComponent("model", {
  type: "box"
});


// Create camera entity
var camera = new pc.Entity();
camera.addComponent("camera", {
  clearColor: new pc.Color(0.1, 0.1, 0.1)
});

// Create directional light entity
var light = new pc.Entity();
light.addComponent("light");

// Add to hierarchy
app.root.addChild(cube);
app.root.addChild(camera);
app.root.addChild(light);

// Set up initial positions and orientations
camera.setPosition(0, 0, 3);
light.setEulerAngles(45, 0, 0);

app.on('update', function(dt) {
    if (app.keyboard.wasPressed(pc.KEY_LEFT)) {
        cube.rotate(0, -20*dt, 0);
    }
});

Wow, thanks! I would’ve never thought of that myself. Didn’t know you could initialize the keyboard that way.
Thanks!

It’s all in the API Reference Manual. :smiley:

https://developer.playcanvas.com/en/api/pc.Application.html#Application

1 Like

Hi @will,

I am not really following the above example. I am having the same 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/