Controlp5 - Multiple buttons that use one function

So let’s say I had a class that’s display was a button.
How would I make it so that all of these classes’ buttons point to one function.


//In my class:
button = cp5.addButton("doThis");

//global scope:
void doThis() {
    print("Hello, world!");

//the error would obviously be this:
Oct 19, 2018 3:52:03 PM controlP5.ControlP5 checkName
WARNING: Controller with name "/doThis" already exists. overwriting reference of existing controller.

I understand why I get an error but I can’t think of an alternative, I guess what I really want is a separate function and name.


Do you mean something like this?

import controlP5.*;

ControlP5 cp5;

void setup() {
  size(800, 400);
  cp5 = new ControlP5(this);
  cp5.addButton("A").setPosition(40, 40);
  cp5.addButton("B").setPosition(40, 60);
  cp5.addButton("C").setPosition(40, 80);

void draw() {

void controlEvent(CallbackEvent event) {
  if (event.getAction() == ControlP5.ACTION_CLICK) {
    switch(event.getController().getAddress()) {
    case "/A":
      println("Button A Pressed");
    case "/B":
      println("Button B Pressed");
    case "/C":
      println("Button C Pressed");

It’s based on the example called ControlP5callback.

No I’m afraid I mean if every button had the same ‘case’.

Thanks anyway.

You need a way to distinguish the buttons in the code. But they can have the same label (so they look identical):

cp5.addButton("A").setLabel("A").setPosition(40, 40);
cp5.addButton("B").setLabel("A").setPosition(40, 60);
cp5.addButton("C").setLabel("A").setPosition(40, 80);

Also not what I mean, but I have an alternative to my problem because I am seeing that what I want isn’t possible: Controlp5 - Buttons with private functions by using a private function.

Ok, I tried :slight_smile: Maybe you can share what you were trying to do and how you solved it, so others can benefit from your discovery :slight_smile:

I will do when I find my solution :slight_smile:

I’m not sure how that relates to my problem, please elaborate.

I believe you can point all your buttons to the same function via plugTo(). Didn’t test it though. :crazy_face:

No I don’t think .plugTo works with buttons, my alternate problem is this one: Controlp5 - Buttons with private functions

I got found a solution, I had the buttons all be called "doThis" + UID and then added some code that tested if ‘doThis’ was in the name on the listener and ran the code like that.

Hey there, I’m running into a similar issue:

I have an object that generates buttons with a method, but I want all of those buttons to do the same thing. There is not a finite amount of buttons that could be generated, so I can’t just create a bunch of similar functions with different names to be linked to buttons with different names.

Anyways, I’m a bit confused by your solution, do you mind elaborating on it? Do you perchance have your original code on hand? Any help would be greatly appreciated :slight_smile: