[SOLVED] Mouse isPressed and wasPressed

I want to start an action right after the mouse is released.

First I tried this:

if (this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
    // start action

But then it also start when the mouse is pressed.

After that I tried this:

if (this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT && !this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
    // start action

But then it never start.

Somebody an idea?

Hi @Albertos,

You can use the MOUSEUP event and subscribe to that to get what you want, put this in your initialize method not in the update:

this.app.mouse.on(pc.EVENT_MOUSEUP, function(event){

   if (event.button === pc.MOUSEBUTTON_LEFT) {
      // left click button was released

}, this);

That is difficult since I want to use it in the update because of other scripting requirements.

I think the wasPressed method doesn’t work as you would expect.
If I was 28 years old, It means I’m not 28 years old anymore. :partying_face:

1 Like

Well, you can easily use it in your update method by assigning it in a script property like this:

this.app.mouse.on(pc.EVENT_MOUSEUP, function(event){

   if (event.button === pc.MOUSEBUTTON_LEFT) {
      this.mouseLeftReleased = true;

}, this);

And make sure to clear it at the end of your update loop:

MyScript.prototype.update = function(dt){

   // update loop end
   this.mouseLeftReleased = false;
1 Like

Thank you @Leonidas, that would indeed be the solution, but it seems that in my situation I can also use wasReleased.

1 Like

What is wasReleased when you use touch input?

So this line for touch input:


You have to listen for the touch end event.

Ah yes, offcourse. Thank you @yaustar.

Hmm, how can I do that inside the update?

Try this in update function, this should work

if (this.app.mouse.wasReleased((pc.MOUSEBUTTON_LEFT)))
        console.log("Mouse was released");

Yes, thats working @saif, but I need the same also for touch input. :see_no_evil:

1 Like

You can’t in the update. You would have to store a flag from the event callback for touch end, and then check against the flag in update.

Same as this basically Mouse isPressed and wasPressed

I have used this touch event on initialize i guess it would work fine in update too

this.app.touch.on(pc.EVENT_TOUCHEND, this.anyFunction, this);

But as @yaustar said it’s not a good practice to register any event again and again in update. You can do it on initialize and both mouse and touch function will work perfectly fine.

Okay, I just wanted to know. Then I have to add something like that.

It is only with certain conditions, so this will not be continuously checked in the update.

1 Like

Fine then, do tell if touch function works for you :slight_smile:

If I understand correctly this will be something like this?


this.isReleased = false;
this.app.touch.on(pc.EVENT_TOUCHEND, this.released, this);


Script.prototype.released = function() {
    this.isReleased = true;
        this.isReleased = false;
    }, 100);

yes, it should work.

I would clear the flag at the end of update. Example: https://playcanvas.com/editor/scene/936587

// The touch event API can be found at http://developer.playcanvas.com/en/api/pc.TouchEvent.html
var TouchEnd = pc.createScript('touchEnd');

TouchEnd.attributes.add("cameraEntity", {type: "entity", title: "CameraEntity"});

// initialize code called once per entity
TouchEnd.prototype.initialize = function() {
    if (this.app.touch) {
        this.app.touch.on(pc.EVENT_TOUCHEND, this.onTouchEnd, this);

// update code called every frame
TouchEnd.prototype.update = function(dt) {
    if (this._touchEndThisFrame) {
        var cameraPanScript = this.cameraEntity.script.cameraPan;
    this._touchEndThisFrame = false;

TouchEnd.prototype.onTouchEnd = function(touchEvent) {
    if (touchEvent.touches.length > 1) {
    this._touchEndThisFrame = true;

1 Like

Just applied and it works fine! Thanks everyone.

1 Like