The BLOCKS SDK
Getting touch events

Touch events are communicated from BLOCKS devices to your application code via TouchSurface objects.

You can obtain a pointer to the TouchSurface associated with a specific BLOCKS device from its corresponding Block object using the Block::getTouchSurface() method — see the Discovering BLOCKS page for an example of how to obtain Block objects. For devices without a touch surface (such as the Control Block) this method will return nullptr, but if the device is capable of sending touch events then the pointer to the TouchSurface will be valid for the lifetime of the Block object.

Once you have a TouchSurface you must register as a TouchSurface::Listener to get touch events. The process for doing this is to have one of your application's classes inherit from TouchSurface::Listener and override the pure virtual method TouchSurface::Listener::touchChanged(). Then, when you register your derived class as a listener to a particular TouchSurface, your overriden method will be called when the corresponding device is touched.

A safe way of registering a class derived from TouchSurface::Listener with a TouchSurface is as follows.

class TouchSurfaceListenerExample : public TouchSurface::Listener
{
public:
TouchSurfaceListenerExample (Block& block)
{
if (auto touchSurface = block.getTouchSurface())
touchSurface->addListener (this);
}
void touchChanged (TouchSurface& sourceTouchSurface, const TouchSurface::Touch& touchEvent) override
{
// Do something with touchEvent here!
}
};

When your overriden touchChanged() method is called you have access to two parameters: a reference to the TouchSurface that generated this event and a reference to a TouchSurface::Touch. The TouchSurface::Touch class contains member variables describing the position, pressure, velocity, timestamp and more.

Example usage

To add this functionality to the BlockFinder example project, add TouchSurface::Listener as a base class to the BlockFinder class and override the touchChanged() function as follows:

class BlockFinder : private TopologySource::Listener,
{
//...
private:
//...
void touchChanged (TouchSurface& sourceTouchSurface, const TouchSurface::Touch& touchEvent) override
{
Logger::writeToLog ("Touch Changed!");
}
//...
};

Then in the topologyChanged() callback, add the BlockFinder class as a listener to the connected Blocks in the current topology to receive touch change callbacks as shown below:

void topologyChanged() override
{
//...
for (auto& block : currentTopology.blocks)
{
//...
if (auto touchSurface = block->getTouchSurface())
touchSurface->addListener (this);
}
}

If you run the application now and connect a Lightpad, you should see text in the logger whenever the surface is touched.

You can also find multiple examples of using TouchSurfaces in the Example JUCE Applications pages.

Learn more about other Block methods from the following pages:

Getting control button events

Controlling LED grids

Controlling LED strips

Controlling control buttons

TouchSurface::Listener
Receives callbacks when a touch moves or changes pressure.
Definition: juce_TouchSurface.h:109
Logger::writeToLog
static void JUCE_CALLTYPE writeToLog(const String &message)
Writes a string to the current logger.
TouchSurface::Listener::touchChanged
virtual void touchChanged(TouchSurface &, const Touch &)=0
TouchSurface
Represents the touch surface of a BLOCKS device.
Definition: juce_TouchSurface.h:32
TouchSurface::Touch
Structure used to describe touch properties.
Definition: juce_TouchSurface.h:42
Block::getTouchSurface
virtual TouchSurface * getTouchSurface() const =0
If this block has a pressure-sensitive surface, this will return an object to access its data.
TopologySource::Listener
Used to receive callbacks for topology changes.
Definition: juce_TopologySource.h:49
Block
Represents an individual BLOCKS device.
Definition: juce_Block.h:32