How to dismiss context menu OSX programmatically in C++

I currently implemented code in C++ with wxWidgets. The code supports Windows and Mac (via xCode). I currently have code in Windows where I use EndMenu (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-endmenu) to dismiss all context menus when an event happens. I need this in order for my UI tasks to continue when certain events such as device connection occurs. I have an issue where having a context menu on screen blocks the event until it is dismissed by the user manually.

Is there an equivalent in OSX for EndMenu or a simple way to dismiss all context menus? The ones that I thought I could use on wxWidgets does not dismiss the context menus at all.

Thanks