CGStreamDisplay and CGEvents for keyboard not working during LoginWindow in intel Mac

I have a remote application I'm working on.

I have a LaunchAgent triggered by a plist stored in /Library/LaunchAgents that is in charge of injecting the mouse and keyboard events and in charge of capturing the monitor. When user is logged in everything works great but when I log out is when I run into issues. I'm certain my agent is running because I can receive and inject the mouse events but:

I'm unable to capture the monitor, the CGDisplayStream init returns nil. I'm unable to post any keyboard events.

The interesting part is that it has worked as expected sometimes but that is probably one out of 10 attempts maybe. Another interesting part is that I see this issue only in a intel Mac but it does not happen at all on my m1 Mac.

I'm also certain that all security permissions have been granted to the Agent (Input Monitoring, Screen Capture, Accessibility) otherwise it would work when user is logged either.

I'm not posting any code since that is not likely to be the issue since it "works" in some Macs and when user is logged in but happy to do so if we need to.

Thank you in advance.

*otherwise it wouldn’t work when the user is logged in. I think there needs to be an option to edit your post 😆

CGStreamDisplay and CGEvents for keyboard not working during LoginWindow in intel Mac
 
 
Q