The BLOCKS SDK
Getting control button events

Control button events are communicated from Lightpad and Control Blocks to your application via ControlButton objects.

You can obtain an array of ControlButton pointers associated with a specific Lightpad or Control Block from its corresponding Block object using the Block::getButtons() method — see the Discovering BLOCKS page for an example of how to obtain Block objects. Each pointer to a ControlButton will be valid for the lifetime of the Block object.

Once you have a ControlButton you must register as a ControlButton::Listener to receive button pressed and button released callbacks. The process for doing this is to have one of your application's classes inherit from ControlButton::Listener and override the pure virtual methods ControlButton::Listener::buttonPressed() and ControlButton::Listener::buttonReleased(). Then, when you register your derived class as a listener to a particular ControlButton, your overriden methods will be called when the corresponding button is pressed and released.

Registering a class derived from ControlButton::Listener with multiple ControlButton objects is done as follows:

class ControlButtonListenerExample : public ControlButton::Listener
{
public:
ControlButtonListenerExample (Block& block)
{
for (auto button : block.getButtons())
button->addListener (this);
}
void buttonPressed (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
{
// Do something when the sourceControlButton is pressed!
}
void buttonReleased (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
{
// Do something when the sourceControlButton is released!
}
};

When your overriden buttonPressed() or buttonReleased() methods are called you have access to two parameters: a reference to the ControlButton that generated this event and timestamp for the event.

Example usage

To add this functionality to the BlockFinder example project, add ControlButton::Listener as a base class to the BlockFinder class and override the buttonPressed() and buttonReleased() functions as follows:

class BlockFinder : private TopologySource::Listener,
{
//...
private:
//...
void buttonPressed (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
{
Logger::writeToLog ("Button Pressed!");
}
void buttonReleased (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
{
Logger::writeToLog ("Button Released!");
}
//...
};

Then in the topologyChanged() callback, add the BlockFinder class as a listener to all the buttons on connected Blocks in the current topology to receive button pressed and button released callbacks as shown below:

void topologyChanged() override
{
//...
for (auto& block : currentTopology.blocks)
{
//...
for (auto button : block->getButtons())
button->addListener (this);
}
}

If you run the application now and connect a Lightpad or Control Block, you should see text in the logger whenever buttons are pressed or released.

You can also find multiple examples of control button listeners in the Example JUCE Applications pages.

Learn more about other Block methods from the following pages:

Getting touch events

Controlling LED grids

Controlling LED strips

Controlling control buttons

Logger::writeToLog
static void JUCE_CALLTYPE writeToLog(const String &message)
Writes a string to the current logger.
ControlButton
Represents a button on a block device.
Definition: juce_ControlButton.h:32
ControlButton::Listener::buttonPressed
virtual void buttonPressed(ControlButton &, Block::Timestamp)=0
Called when the button is pressed.
TopologySource::Listener
Used to receive callbacks for topology changes.
Definition: juce_TopologySource.h:49
Block::Timestamp
uint32 Timestamp
This type is used for timestamping events.
Definition: juce_Block.h:488
ControlButton::Listener
A listener that can be attached to a ControlButton object so that it gets called when the button is p...
Definition: juce_ControlButton.h:118
Block
Represents an individual BLOCKS device.
Definition: juce_Block.h:32
ControlButton::Listener::buttonReleased
virtual void buttonReleased(ControlButton &, Block::Timestamp)=0
Called when the button is released.
Block::getButtons
virtual juce::Array< ControlButton * > getButtons() const =0
If this block has any control buttons, this will return an array of objects to control them.