Posting keyboard events to host

I know subject was discussed several times on that forum, but I would like to make some summarisation post, hopefully someone can provide any new solution.
I am little bit heartbroken :slight_smile: because I waisted almost week to find solution how to send “play/stop” command to plugin host. Normally from plugin it is send by clicking keyboard spacebar. But I am working on plugin which has separated window (created with juce::DocumentWindow) and even I set that window to setWantsKeyboardFocus(false), the clicking spacebar is not propagated to host (for example Logic Pro).

I tried to use some system methods like:

CGEventRef keyDown = CGEventCreateKeyboardEvent(**NULL**, kVK_Space, **true**);
CGEventPost(kCGHIDEventTap, keyDown);

But it propagate spacebar to the system, and the system ask me to give permission for such things which I don’t like, but even I give that permission, Logic Pro still doesn’t receive spacebar event (at least “play/stop” doesn’t work).

Lastly I found that there is also something like:
CGEventPostToPid(pid_t pid, CGEventRef event);

I don’t know if it will help. But at the moment I am not even sure how to use it. I tried to set parameter pid_t to AUHostingServiceXPC (which I use to debug in Logic Pro), but unfortunatelly when I use it in the code I get compile error that AUHostingServiceXPC is undeclared. Does any one know what should I #include to be able to useAUHostingServiceXPC in the code?

And - which is more important - will CGEventPostToPid solve my issue? Or should I do anything else?

Is there anyone who know how and if I can send “play/stop” commnd from code to the plugin host?

I should also mention that it is important for me to send “play/stop” command from DocumendWindow even after my main AudioProcessorEditor is destroyed.

For any help great thanks in advance.
Best Regards