Processing ControlP5 example 1: user interface

The controlP5 library for Processing makes it easy to add knobs and buttons to your Processing applications. In this article an example how to add some interface elements (buttons, sliders etc.) and how to listen to their events.

ControlP5 is a GUI and controller library for Processing. You can easy add controllers to your program. You can download the ControlP5 library at the site of Andreas Schlegel. After downloading und unpacking you can copy the library in the ‘libraries’ subfolder of the Processing sketches folder.

The library comes with a lot of examples, but it lacks a bit an overview how to use different elements together. In this article and example an overview of some basic GUI elements and how you can handle their events.

See the sketch in action at openprocessing.org.
Download the zip-file ( with all the examples ) : controlP5-examples.zip

controlP5 basic interface elements
ElementDescription
Bang : A bang controller triggers an event when pressed.

parameters: name, x, y, width, height
controlP5.addBang("bang1",10,10,20,20);
Button : A button executes after release.
parameters: name, value (float), x, y, width, height
controlP5.addButton("button1",1,70,10,60,20);
Toggle : A toggle can have two states, true and false. Where true has the value 1 and false is 0.
parameters: name, default value (boolean), x, y, width, height
controlP5.addToggle("toggle1",false,170,10,20,20);
Slider : A slider is either used horizontally or vertically. When the width is bigger, you get a horizontal slider. When the height is bigger, you get a vertical slider.
parameters: name, minimum, maximum, default value (float), x, y, width, height
controlP5.addSlider("slider2",0,255,128,70,80,100,10);
Knob : A round turning dial knob. Counts from 0-360 degrees.
parameters: name, minimum, maximum, default value (float, x, y, diameter
controlP5.addKnob("knob1",0,360,0,70,120,50);
NumberBox : Box that displays a number. You can change the value by click and hold in the box and drag the mouse up and down.
parameters: name, default value (float), x, y, width, height
controlP5.addNumberbox("numberbox1",50,170,120,60,14);

variables and setup

controlEvent

The function controlEvent is an event listener for all the user elements. In this case we only want to listen to controller events ( if(theEvent.isController()) ). By getting the name ( theEvent.controller().name())) and the value (theEvent.controller().value()) ) of the event we can program some logic (if/else) and change some variables in Processing (in this case the values in the color[] array).

  • Sharul Agrawal

    Hey..

    I am stuck in my project and have some doubt regarding controlp5 library to develop GUI.

    I have put three temperature sensor in my three different rooms and what I want is to create a simple GUI where I can view status of all three rooms wirelessly at my remote PC.

    This processing program will be running at my remote PC.All d wireless connection is done.I have kept three different buttons as “ROOM1”, “ROOM2” and “ROOM3”.. What I want is when I click on “Room1” It sholud display me a box with temperature reading. Just I dnt know how to fire an event through button that it shows me some text field or label showing me reading.

    Rest all issues of having data at port is already done.

    pls help..:)

    Thanks

  • Well do something in here:

    if(theEvent.controller().name()==”bang1″) {
    // do something here. Change a label or something.
    println(“my temperature”);
    }

  • Jean-François Renaud

    About string comparaison in the controlEvent function. I noticed that you use ==. Even if it seems to work, I thought it should be better to use .equals(), as suggested in the reference file. It is reported that the equality only check if both strings “are stored in the same memory location”.
    http://www.processing.org/reference/String.html

  • Armando Marques Sobrinho

    I put two textfield in my sketch, but I need it to be setted to noloop, but because that, the control dont work, how I can solve this?

    thank you!

  • You shouldn’t use noLoop(). You have to find a way to not update or change your contents in the draw loop. But since I don’t know your code I can’t give you specific advise right now. Maybe you can link your code (pastbin or something) so I see what your problem is.

  • Armando Marques Sobrinho

    ok, thank you for reply

    here is my code in pastebin

    http://pastebin.com/a6M9zQ1m

  • Do something like:

    boolean hasBeenDrawn = false;

    void setup()

    {

    }

    void draw()

    { if(hasBeenDrawn == false)

    { // code in draw loop

    hasBeenDrawn = true;

    }

    }

    http://pastebin.com/fMuB7JL9

  • Armando Marques Sobrinho

    thank you, I try to dont use noloop, but how I need to be in a loop, and because its controls is to get the information for a user, so I only will drw what I need after the user pass these information, and so it will solve my problem.

  • Armando Marques Sobrinho

    another question.
    if you figure out, I use a class, have a way to pass a bang to draw it inside of my class?

    thank’s

  • Armando Marques Sobrinho

    Im can use tab or enter to navigate beween controlers?
    And How I can set another mouse cursor?

    thank you.

  • Armando Marques Sobrinho

    Im Solved that, my friend, its so simple, I put my loop to draw my object in another function out of the draw() function.
    it ok now.

  • Armando Marques Sobrinho

    its dont is necessary in this moment, boot the question is still open.

  • Deepankar Maithani

    Hi Kasper

    i am using this library and i am having problem returning the value of knob to the draw() function. Each time user turns the knob a value is there when i try to print that value in within the speed function. but i am unable to pass it to the draw(). Pls help

    import controlP5.*;

    int speedData;

    int knobeventdata;

    Knob myKnobB;

    ControlP5 cp5;

    void setup()

    {

    size(500,500);

    cp5 = new ControlP5(this);

    myKnobB = cp5.addKnob(“SPEED”)

    .setRange(0,255)

    .setValue(220)

    .setPosition(100,100)

    .setRadius(50)

    .setNumberOfTickMarks(2)

    .setTickMarkLength(4)

    .snapToTickMarks(true)

    .setColorForeground(color(255))

    .setColorBackground(color(0, 160, 100))

    .setColorActive(color(255,255,0))

    .setDragDirection(Knob.HORIZONTAL)

    ;

    }

    void draw()

    {

    speedData=SPEED( knobeventdata);

    println(speedData);

    }

    void keyPressed()

    {

    if(key==’W’)

    {

    switch(speedData)

    {

    case 0:

    println(“speed is zero”);

    break;

    case 127:

    println(“speed is 127”);

    break;

    case 255:

    println(“speed is 255”);

    break;

    }

    }

    }

    int SPEED(int valKnob) {

    //println(valKnob);

    return valKnob;

    }

  • Rafał Szu

    How can I change size of text (e.g. position of slider) and orientation of app?