# How to active ControlEvent in ControlP5 - python Mode?

**URL:** <https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113>\
**Category:** Processing.py\
**Created:** [June 20, 2018, 5:59am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113 "2018-06-20T05:59:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 20, 2018, 5:59am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/1 "2018-06-20T05:59:27Z")

</div>

in Java language, here is the original code:

> import controlP5.\*;
> 
> ControlP5 cp5;  
> int myColor = color(0, 0, 0);
> 
> int sliderValue = 100;  
> int sliderTicks1 = 100;  
> void setup() {  
> size(700, 400);  
> noStroke();  
> cp5 = new ControlP5(this);
> 
> cp5.addSlider(“slider”)  
> .setPosition(100, 305)  
> .setSize(200, 20)  
> .setRange(0, 200)  
> .setValue(128)  
> ;  
> }
> 
> void draw() {  
> background(0);  
> }
> 
> void slider(float theColor) {  
> myColor = color(theColor);  
> println("a slider event. setting background to "+theColor);  
> }

It work well: When I change the slider, the “void slider” will be activated and print what I want to.

But when I use python to write this, the “def slider” or other ControlEvent will not active. How to make ControlP5 work correctly in python Mode? Here’s the code below:

> add\_library(‘controlP5’)  
> def setup():  
> size(700,400)  
> noStroke()  
> global slider  
> cp5 = ControlP5(this)
> 
> ```
> slider = cp5.addSlider("slider")
> slider.setPosition(100, 305)
> slider.setSize(200, 20)
> slider.setRange(0, 200)
> slider.setValue(128)
> 
> ```
> 
> def draw():  
> background(0)
> 
> def slider(theColor):  
> myColor = color(theColor)  
> print("a slider event.setting background to " + theColor)

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [June 20, 2018, 8:27am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/2 "2018-06-20T08:27:10Z")

</div>

I think you’re looking for the `.value` method:

```auto
def setup():
    global slider
    size(700,400, P2D)
        
    cp5 = ControlP5(this)
    slider = cp5.addSlider("slider").setPosition(100, 305).setSize(200, 20).setRange(0, 200).setValue(128)

def draw():
    background(slider.value)
    print slider.value

```

---

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 20, 2018, 8:42am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/3 "2018-06-20T08:42:15Z")

</div>

Thank you solub, actually I want to active a function while slider value changed.  
Maybe in JAVA , `void ControlEvent` can work. But in python mode `def ControlEvent` will not be avtivated.

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [June 20, 2018, 8:50am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/4 "2018-06-20T08:50:52Z")

</div>

Could you please elaborate further on what you’re trying to achieve ? What is that `slider` function for exactely ?

---

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 20, 2018, 8:57am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/5 "2018-06-20T08:57:56Z")

</div>

In JAVA code:

> import controlP5.\*;  
> ControlP5 cp5;  
> Slider slider;  
> int myColor = color(0, 0, 0);  
> int sliderValue = 100;  
> int sliderTicks1 = 100;  
> void setup() {  
> size(700, 400);  
> noStroke();  
> cp5 = new ControlP5(this);  
> slider = cp5.addSlider(“slider”)  
> .setPosition(100, 305)  
> .setSize(200, 20)  
> .setRange(0, 200)  
> .setValue(128)  
> ;  
> }  
> void draw() {  
> background(0);  
> }
> 
> void controlEvent(ControlEvent theEvent){  
> if (theEvent.isFrom(slider)){  
> myFunction();  
> }  
> }

when I change the slider value, it will activate `myFunction()`. Actually in `myFunction` I want to use a slider to control the controlP5 Group’s number in realTime. But nothing happend in python mode:

> add\_library(‘controlP5’)  
> def setup():  
> size(700,400)  
> noStroke()  
> global slider  
> cp5 = ControlP5(this)  
> slider = cp5.addSlider(“slider”)  
> slider.setPosition(100, 305)  
> slider.setSize(200, 20)  
> slider.setRange(0, 200)
> 
> def draw():  
> background(0)
> 
> def controlEvent(theEvent):  
> global slider  
> if (theEvent.isFrom(slider)):  
> myFunction()

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 20, 2018, 9:09am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/6 "2018-06-20T09:09:32Z")

</div>

```auto
"""
 ControlP5::addListener() (v1.1)
 GoToLoop (2018-Jun-20)

 https://Discourse.Processing.org/t/
 how-to-active-controlevent-in-controlp5-python-mode/1113/6
"""

add_library('controlP5')

bg = 0200

def setup():
    size(400, 300)

    global slider
    slider = ControlP5(this).\
             addSlider('slider').\
             setSize(width>>1, 20).\
             setPosition(width>>2, height>>1).\
             setRange(0, 0xff).\
             setValue(bg).\
             addListener(sliderListener)

def draw(): background(bg)

def sliderListener(evt):
    global bg
    bg = int(evt.getValue())

```

---

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 20, 2018, 9:46am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/7 "2018-06-20T09:46:24Z")

</div>

Thank you GoToLoop! That works.  
One more step if you don’t mind, what if I want to listen the specific slider for example: Slider01?

And where can I get the information that I can use `def sliderListener` to listen sliders? I can’t find this function in [controlP5 website libraries](http://www.sojamo.de/libraries/controlP5/reference/index.html)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 20, 2018, 10:20am UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/8 "2018-06-20T10:20:39Z")

</div>

> [@newton45](#):
>
> … what if I want to listen the specific slider for example: _Slider01_?

Your Java version relies on method **isFrom()**. So that’s what I did now: 😏

```auto
"""
 ControlP5::addListener() (v1.2)
 GoToLoop (2018-Jun-20)

 https://Discourse.Processing.org/t/
 how-to-active-controlevent-in-controlp5-python-mode/1113/8
"""

add_library('controlP5')

bg = 0200

def setup():
    size(400, 300)

    global slider
    slider = ControlP5(this).\
             addSlider('slider').\
             setSize(width>>1, 20).\
             setPosition(width>>2, height>>1).\
             setRange(0, 0xff).\
             setValue(bg).\
             addListener(sliderListener)

def draw(): background(bg)

def sliderListener(evt):
    global bg
    if evt.isFrom(slider): bg = int(evt.getValue())

```

> [@newton45](#):
>
> And where can I get the information that I can use `def sliderListener(evt):` to listen sliders?

I’ve found that out at “Python Mode Examples”. 😸  
Although I already knew about the `class` extending ControlListener approach. 😤

Hit CTRL+SHIFT+O, then go to “Contributed Libraries in Python” → “ControlP5” → “Textfield”. 🧐

---

<div class="post-metadata">

**Author:** ![newton45](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@newton45](https://discourse.processing.org/u/newton45)\
**Post date:** [June 20, 2018, 12:38pm UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/9 "2018-06-20T12:38:04Z")

</div>

I found a new way to achieve that, bro:

> Slider01 = ControlP5(this).  
> …  
> addListener(youCanNameThis)
> 
> def youCanNameThis(evt):  
> function()

That’s listen to Slider01 only.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 20, 2018, 12:50pm UTC](https://discourse.processing.org/t/how-to-active-controlevent-in-controlp5-python-mode/1113/10 "2018-06-20T12:50:08Z")

</div>

> [@newton45](#):
>
> That’s listen to _Slider01_ only.

But my sketch already just listens to _slider_ only! 🙄
