Question on Button releasing. Using CP5

FemFem
edited August 2015 in Library Questions

How do you make an action occur only once a button is released? NOTE: Regards to Cp5 button

Answers

  • boolean on = false;
    
    void draw()
    {
    if(button.isMousePressed()==true){on = true;}
    if(on == true && button.isMousePressed() == false){on = false; action();}
    }
    
    void action(){}
    

    mayby it help you

  • Hi, for a button you can call activateBy(), see example, also documented in button class source code.

        import controlP5.*;
    
        ControlP5 cp5;
    
        void setup() {
          size(400,400);
          cp5 = new ControlP5(this);
          cp5.addButton("button").setSize(100,100).activateBy(ControlP5.RELEASE);
        }
    
        void draw() {}
    
        void controlEvent(ControlEvent theEvent) {
          println("event from ", theEvent.getController().getName());
        }
    

    you can also use a Callback to capture events from a controller, like in the example below.

    import controlP5.*;
    
    ControlP5 cp5;
    int bg = 0;
    
    void setup() {
      size(400, 400);
      cp5 = new ControlP5(this);
      cp5.addButton("button").setSize(100, 100);
      cp5.addCallback(new CallbackListener() {
        public void controlEvent(CallbackEvent theEvent) {
          // see the Callback example for more details
          switch(theEvent.getAction()) {
            case(ControlP5.ACTION_RELEASE):
            println("controller release", theEvent.getController().getName());
            bg = int(random(255));
            break;
            case(ControlP5.ACTION_PRESS):
            println("controller press", theEvent.getController().getName());
            break;
          }
        }
      }
      );
    }
    
    void draw() {
      background(bg);
    }
    
Sign In or Register to comment.