The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.7KB

  1. /**
  2. @page getting_control_button_events Getting control button events
  3. Control button events are communicated from Lightpad and Control Blocks to your application via ControlButton objects.
  4. 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 @ref discovering_blocks page for an example of how to obtain %Block objects.
  5. Each pointer to a %ControlButton will be valid for the lifetime of the %Block object.
  6. Once you have a %ControlButton you must register as a ControlButton::Listener to receive button pressed and button released callbacks.
  7. 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().
  8. 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.
  9. Registering a class derived from %ControlButton::Listener with multiple %ControlButton objects is done as follows:
  10. @code{.cpp}
  11. class ControlButtonListenerExample : public ControlButton::Listener
  12. {
  13. public:
  14. ControlButtonListenerExample (Block& block)
  15. {
  16. for (auto button : block.getButtons())
  17. button->addListener (this);
  18. }
  19. void buttonPressed (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
  20. {
  21. // Do something when the sourceControlButton is pressed!
  22. }
  23. void buttonReleased (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
  24. {
  25. // Do something when the sourceControlButton is released!
  26. }
  27. };
  28. @endcode
  29. When your overriden @s_projcode{buttonPressed()} or @s_projcode{buttonReleased()} methods are called you have access to two parameters: a reference to the %ControlButton that generated this event and timestamp for the event.
  30. @section getting_control_button_events_example_usage Example usage
  31. To add this functionality to the BlockFinder example project, add %ControlButton::Listener as a base class to the BlockFinder class and override the @s_projcode{buttonPressed()} and @s_projcode{buttonReleased()} functions as follows:
  32. @code{.cpp}
  33. class BlockFinder : private TopologySource::Listener,
  34. private ControlButton::Listener
  35. {
  36. //...
  37. private:
  38. //...
  39. void buttonPressed (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
  40. {
  41. Logger::writeToLog ("Button Pressed!");
  42. }
  43. void buttonReleased (ControlButton& sourceControlButton, Block::Timestamp timestamp) override
  44. {
  45. Logger::writeToLog ("Button Released!");
  46. }
  47. //...
  48. };
  49. @endcode
  50. Then in the @s_projcode{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:
  51. @code{.cpp}
  52. void topologyChanged() override
  53. {
  54. //...
  55. for (auto& block : currentTopology.blocks)
  56. {
  57. //...
  58. for (auto button : block->getButtons())
  59. button->addListener (this);
  60. }
  61. }
  62. @endcode
  63. 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.
  64. You can also find multiple examples of control button listeners in the @ref example_applications pages.
  65. Learn more about other Block methods from the following pages:
  66. @ref getting_touch_events
  67. @ref controlling_led_grids
  68. @ref controlling_led_strips
  69. @ref controlling_control_buttons
  70. */