Is there a way to have button trigger on the press of the button and not the release?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-09-12 6:32 AM
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?
Solved! Go to Solution.
- Labels:
-
TouchGFX
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-09-12 7:23 AM
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);
}
}
...
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2019-09-12 7:23 AM
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);
}
}
...
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-06-11 1:43 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-10-16 5:02 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-10-19 1:07 AM
It's definitely a less clean solution, but kudos to @RGajd.1​ for looking for alternative solutions :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-01-26 10:29 AM
Hi @Martin KJELDSEN​ , can I do the same with the Flexbutton? I cannot find the equivalent to AbstractButton.cpp.
Only found AbstractButtonContainer.hpp.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-01-27 1:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-06-23 8:21 PM
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.
