# ToggleButton - is there any lambda to assign when toggle state is changed?

**URL:** <https://forum.juce.com/t/togglebutton-is-there-any-lambda-to-assign-when-toggle-state-is-changed/49947>\
**Category:** General JUCE discussion\
**Created:** [February 1, 2022, 2:29pm UTC](https://forum.juce.com/t/togglebutton-is-there-any-lambda-to-assign-when-toggle-state-is-changed/49947 "2022-02-01T14:29:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [February 1, 2022, 2:29pm UTC](https://forum.juce.com/t/togglebutton-is-there-any-lambda-to-assign-when-toggle-state-is-changed/49947/1 "2022-02-01T14:29:24Z")

</div>

Hello,  
I have problem with `juce::ToggleButton` because I always was sure that `onStateChange` is called when toggle state of button is changed. But it is called every mouse enter, exit, down etc.

There is also lambda `onClick`, but it is called everytime I click the button, no matter if toggle state was changed or not.

But I am looking for something that would be called only when toggle state is changed

Is there anything like that in `juce::ToggleButton`, or in `juce::Button`?

For any help great thanks in advance.

Best Regards

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [February 1, 2022, 2:40pm UTC](https://forum.juce.com/t/togglebutton-is-there-any-lambda-to-assign-when-toggle-state-is-changed/49947/2 "2022-02-01T14:40:49Z")

</div>

`onClick` will be called when the toggle state is changed _if the second argument to `setToggleState()` is set to send notifications_. [It’s always called just after the listeners are called - if the listeners aren’t called, `onClick` won’t be called](https://github.com/juce-framework/JUCE/blob/0abbba3b18c3263137eeaeaa11c917a3425ce585/modules/juce_gui_basics/buttons/juce_Button.cpp#L425).

There’s no way to get a callback from a button when it’s toggle state changes but the notification type was set to `dontSendNotification` - that’s the whole point of that second argument.

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [February 1, 2022, 2:44pm UTC](https://forum.juce.com/t/togglebutton-is-there-any-lambda-to-assign-when-toggle-state-is-changed/49947/3 "2022-02-01T14:44:41Z")

</div>

Think I misread your post originally - as you say, `onClick` is called regardless of whether or not the state has changed - same for the `buttonClicked()` callback for listeners.

You’d have to manually keep track of the button’s state and check if it’s changed in the callbacks so you can act accordingly

---

<div class="post-metadata">

**Author:** ![pajczur](https://avatars.discourse-cdn.com/v4/letter/p/e9a140/32.png) [@pajczur](https://forum.juce.com/u/pajczur)\
**Post date:** [February 1, 2022, 2:45pm UTC](https://forum.juce.com/t/togglebutton-is-there-any-lambda-to-assign-when-toggle-state-is-changed/49947/4 "2022-02-01T14:45:56Z")

</div>

OK, thanks for your explanation.
