2020-12-08 07:54 PM
I was wondering if it was possible to create a momentary button on a touchscreen using touchGFX? I am interested in using it to activate a pump while pressing the screen. For my application, a momentary button would be better suited than any latching switch.
Cheers
Solved! Go to Solution.
2020-12-09 11:07 AM
You can add a ClickListener mixin to any element*. An element with a ClickListener will fire a callback with a "ClickEvent" type - these types include "pressed", "released" or "cancel" (was pressed, but the finger was slid off rather than lifted).
* Including a button! The callbacks emitted by the standard button widgets don't seem to differentiate between the events and only fire when the button was pressed and then released, so you need the clicklistener mixin to receive a callback when the state is changed. I've not tested this though.
Example implementation:
if (evt.getType() == ClickEvent::PRESSED) {
// do what you need to do when the button is held down
} else if(evt.getType() == ClickEvent::RELEASED){
// do what you need when the button is released
} else {
// here the button was cancelled (finger slid off the button)
}
If you follow the MVP design pattern, your code for handling each event could call a method in the Presenter (such as presenter->buttonDown(), or presenter->buttonUp()) which would then call functions in the model that actually deal with the hardware (e.g. Model::setPumpStatus() ).
Alternately, you could probably extend the AbstractButton class, which will allow you to override AbstractButton::handleClickEvent(const ClickEvent & event). In this method you would check the event as the above, except instead of calling code directly, you would fire a callback with a parameter that passes the ClickEvent, but unless you have a bunch of buttons like this it might not be worth it. You also wouldn't be able to place them using the Designer.
2020-12-09 07:03 AM
??? Place Button with Label and do methods
2020-12-09 11:07 AM
You can add a ClickListener mixin to any element*. An element with a ClickListener will fire a callback with a "ClickEvent" type - these types include "pressed", "released" or "cancel" (was pressed, but the finger was slid off rather than lifted).
* Including a button! The callbacks emitted by the standard button widgets don't seem to differentiate between the events and only fire when the button was pressed and then released, so you need the clicklistener mixin to receive a callback when the state is changed. I've not tested this though.
Example implementation:
if (evt.getType() == ClickEvent::PRESSED) {
// do what you need to do when the button is held down
} else if(evt.getType() == ClickEvent::RELEASED){
// do what you need when the button is released
} else {
// here the button was cancelled (finger slid off the button)
}
If you follow the MVP design pattern, your code for handling each event could call a method in the Presenter (such as presenter->buttonDown(), or presenter->buttonUp()) which would then call functions in the model that actually deal with the hardware (e.g. Model::setPumpStatus() ).
Alternately, you could probably extend the AbstractButton class, which will allow you to override AbstractButton::handleClickEvent(const ClickEvent & event). In this method you would check the event as the above, except instead of calling code directly, you would fire a callback with a parameter that passes the ClickEvent, but unless you have a bunch of buttons like this it might not be worth it. You also wouldn't be able to place them using the Designer.
2020-12-09 11:41 AM
Cake Recipe
Sounds easy! :)
2020-12-09 11:46 AM
Using the Clicklistener method worked perfectly!
Thank you so much for you help :thumbs_up:
2020-12-09 11:58 AM
You're welcome :) You can mark the question as answered by selecting "Best Answer" on my first comment.
2021-10-05 09:43 AM
Is there a project example I can download for this Momentary button behavior?