How to call simulate touch on ui::Widget?

I have ui::Button (which extends ui::Widget) and:

btnBonus->addTouchEventListener([&](Ref* ref, ui::Widget::TouchEventType touchEventType){
...
}

And I want to simulate touch (ended) on this button, when back button is touched (on android):

this->setKeyboardEnabled(true);

void MenuScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event *event){
    if(keyCode == EventKeyboard::KeyCode::KEY_BACK){
            if(btnBonus != nullptr){
                Touch* touch = new Touch();
                touch->setTouchInfo(1, btnBonus->getPositionX(), btnBonus->getPositionY());
                btnBonus->onTouchBegan(touch, nullptr);
                btnBonus->onTouchEnded(touch, nullptr);
                CC_SAFE_RELEASE_NULL(touch);
                CCLOG("click sent");
            }
    }
}

I can see “click sent” in logs, but callback is not firing. I just wanted to call callback directly, but it’s protected.

I would just call a common MenuScene::bonusAction() method from within both events instead.
Calling btnBonus->releaseUpEvent() would also work, but it’s a protected method.

Yeah I know that. But I wanted to simulate click :slight_smile:

Have you tried EventDispatcher::dispatchTouchEvent?

Edit: scratch that. Your code works for me?

I got close with creating a vector of Touches, setting the type of the Event then dispatching the event of the widget I’m messing with. Seems like it didn’t work because listeners of the widget weren’t active yet. Not sure, but if you figure it out, let me know. Right now I’m just calling the callback myself outside of cocos, instead of going through the event graph.

Here’s the code that doesn’t work, but maybe it’ll help someone:

EventTouch* evt = new EventTouch();
evt->setEventCode(EventTouch::EventCode::BEGAN);
auto t1 = new Touch();
t1->setTouchInfo(-1, 100, 535);
const std::vector<cocos2d::Touch*> t = { t1 };
evt->setTouches(t);
my_button->getEventDispatcher()->dispatchEvent(evt);

EventTouch* end = new EventTouch();
end->setTouches(t);
end->setEventCode(EventTouch::EventCode::ENDED);
my_button->getEventDispatcher()->dispatchEvent(end);