cancel
Showing results for 
Search instead for 
Did you mean: 

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

WLewe
Associate III

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Martin KJELDSEN
Chief III

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);
        }
    }
...
}

View solution in original post

7 REPLIES 7
Martin KJELDSEN
Chief III

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);
        }
    }
...
}

RGajd.1
Associate II

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

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

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

TTulw.1
Associate II

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

Only found AbstractButtonContainer.hpp.

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

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.