cancel
Showing results for 
Search instead for 
Did you mean: 

how to make some code be called on every button press

Zui
Senior

In my current project i need a buzzer buzz every time any button is pressed, in simulator i'm just using Beep(F,D); but i was looking for a better approach than paste the beep function inside every single function called by any button, besides that won't work in buttons that just change screen, due that the code is in ViewBase File and so can't be modified.

Thanks

Stefano

7 REPLIES 7
Martin KJELDSEN
Chief III

Hi @Zui​,

You could create a custom Button, BeepButton, If you don't want to manually insert a call to Beep() - Which is what i think you're saying. You won't be able to use this button from the designer as a trigger, however.

Let me know.

/Martin

Zui
Senior

i was wondering of a sort of override whatever any "clickable" is calling, but no idea if that was possible

Martin KJELDSEN
Chief III

The Button, specifically, has an internal handler that executes a callback which is typically tied to a handler, as you know. That's all you can do to "override" a standard button.

/Martin

Martin KJELDSEN
Chief III

Here's the handleClickEvent() method for AbstractButton (from which Button inherits).

#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

Zui
Senior

works like a charm, Thanks ^^

works like a charm, Thanks ^^

Zui
Senior

I just realize that my Beep function is called on every screen click, not onl on buttons, but since the handleClickEvent takes only the vent type, and not the event source, i can't figure out how to make the function to be called only when a button is pressed.

Any suggestion?

Thanks

Stefano

EDIT:

i manage to do what i want in a "code injection" way, by adding throu the designer an interaction when the last added button is pressed i execute this piece of code:

}

#ifdef SIMULATOR

Beep(BUZZER_NOTE_MEDIUM, BUZZER_NOTE_DURATION);

#endif // SIMULATOR

presenter->touchFeedBack();

{

so in the viewbase.cpp the buttonCallbackHandler function exits from it's if-else if structure, and execute my code on Every button pression.

I know it's a BAD way of doing this, so any suggestions are welcome