Yes we can! Make a method of the same name as your button. That code will be executed when the button is pressed. You can put the screenshot code in there.
Sidenote: depending on your exact code, this will create a screenshot WITH the controlP5 controllers (like the example below). Sometimes this is exactly what you want. Sometimes you want a screenshot WITHOUT the controlP5 controllers in it. Then you have to set controlP5 autoDraw to false, toggle a boolean with the button/toggle and put your screenshot code in the if (screenShotBoolean==true) check at the end of draw(). You can put the manual controlP5 draw() after that.
Code Example
- import controlP5.*;
- ControlP5 cp5;
-
- void setup() {
- size(400, 600);
- background(0);
- smooth();
- cp5 = new ControlP5(this);
- cp5.addButton("saveScreen").setPosition(100, 100).setSize(200, 100);
- }
-
- void draw() {
- fill(random(255), random(255), random(255));
- ellipse(random(width), random(height), 100, 100);
- }
-
- public void saveScreen() {
- saveFrame();
- }