Skip to main content
WLewe
Associate III
September 12, 2019
Solved

Is there a way to have button trigger on the press of the button and not the release?

  • September 12, 2019
  • 3 replies
  • 3918 views

I built a simple project that turns on a LED light. I would like to have the event trigger on the press of the button and not when I press and release the button. Is there a spot in the code where a simple change can make this happen?

This topic has been closed for replies.
Best answer by Martin KJELDSEN

Hi @WLewe​,

Fortunately, you have the source code available for the Buttons in the framework. The class from which "normal" buttons (tigger on release) inherit is called Abstract Button. As you can see from the source code, it triggers on release.

#include <touchgfx/widgets/AbstractButton.hpp>
 
namespace touchgfx
{
void AbstractButton::handleClickEvent(const ClickEvent& event)
{
 bool wasPressed = pressed;
 pressed = (event.getType() == ClickEvent::PRESSED);
 if ((pressed && !wasPressed) || (!pressed && wasPressed))
 {
 // Pressed state changed, so invalidate
 invalidate();
 }
 if (wasPressed && (event.getType() == ClickEvent::RELEASED) && action)
 {
 // This is a click. Fire callback.
 if (action->isValid())
 {
 action->execute(*this);
 }
 }
}
} //namespace touchgfx

You can create your own Button base class that does something else. In a way, you could say that AbstractButton could be called TriggerOnReleasedButton, or something like that.

Here's some pseudocode for your new TriggerOnPressedButton base class.

#include <gui/common/TriggerOnPressedButton.hpp>
 
void TriggerOnPressedButton::handleClickEvent(const ClickEvent& event)
{
 ...
 if ( (event.getType() == ClickEvent::PRESSED) && action)
 {
 // ButtonPress. Fire callback.
 if (action->isValid())
 {
 action->execute(*this);
 }
 }
...
}

3 replies

Martin KJELDSEN
Martin KJELDSENBest answer
Principal III
September 12, 2019

Hi @WLewe​,

Fortunately, you have the source code available for the Buttons in the framework. The class from which "normal" buttons (tigger on release) inherit is called Abstract Button. As you can see from the source code, it triggers on release.

#include <touchgfx/widgets/AbstractButton.hpp>
 
namespace touchgfx
{
void AbstractButton::handleClickEvent(const ClickEvent& event)
{
 bool wasPressed = pressed;
 pressed = (event.getType() == ClickEvent::PRESSED);
 if ((pressed && !wasPressed) || (!pressed && wasPressed))
 {
 // Pressed state changed, so invalidate
 invalidate();
 }
 if (wasPressed && (event.getType() == ClickEvent::RELEASED) && action)
 {
 // This is a click. Fire callback.
 if (action->isValid())
 {
 action->execute(*this);
 }
 }
}
} //namespace touchgfx

You can create your own Button base class that does something else. In a way, you could say that AbstractButton could be called TriggerOnReleasedButton, or something like that.

Here's some pseudocode for your new TriggerOnPressedButton base class.

#include <gui/common/TriggerOnPressedButton.hpp>
 
void TriggerOnPressedButton::handleClickEvent(const ClickEvent& event)
{
 ...
 if ( (event.getType() == ClickEvent::PRESSED) && action)
 {
 // ButtonPress. Fire callback.
 if (action->isValid())
 {
 action->execute(*this);
 }
 }
...
}

Xxoyo.1
Associate III
June 24, 2021

Hello, I would like to create my own Button base class for this , but how should I approach it? Do I create a .cpp file ,place the above code , and make the button inherit this functionality ? Or do I modify my ScreenView.cpp file to override the button's handleClickEVent? Any tutorials is appreciated !

Update:

I have used the Mixins ClickListener on the button to achieve the Pressed & Released events. It is triggering the pressed / released correctly.

RGajd.1
Associate
June 11, 2020

Hi @WLewe​ 

I think there is an easier way to reach the goal :

Use flexButton and choose trigger as Touch.

Then in handleTickEvent Your screenView You can check if flexButton is pressed or released.

Pseudocode;

void screenView::handleTickEvent() {

if( myFlexButton.getPressed() ) LedOn();

else LedOff();

}

Regards

chaaalyy
Senior II
October 16, 2020

Wouldn´t this fire the callback every Tick ?!?

I think, Martin´s approach fires the action only once (when clickEvent is handled...).

Maybe just a small difference in case of just switching something to an absolute defined state (on or off), but anyway: if the "action" is something like "toggle", you would be in trouble ;)

/Charly

Martin KJELDSEN
Principal III
October 19, 2020

It's definitely a less clean solution, but kudos to @RGajd.1​ for looking for alternative solutions :)

TTulw.1
Visitor II
January 26, 2021

Hi @Martin KJELDSEN​ , can I do the same with the Flexbutton? I cannot find the equivalent to AbstractButton.cpp.

Only found AbstractButtonContainer.hpp.

Romain DIELEMAN
ST Employee
January 27, 2021

Hi,

The solution RGajd.1 gave above works, are you looking for a cleaner one ? Maybe have a look at ClickButtonTrigger.hpp which is used for flex buttons to call the set action when it gets a touch released event, seems to be the equivalent to AbstractButton.cpp.

/Romain