i want to simulate the keyboard with CGEventPost in the user session (MacOS).
CGEventRef keyEvent = CGEventCreateKeyboardEvent( NULL, keyCode, down ) ;
CGEventPost( kCGHIDEventTap, keyEvent ) ;
CFRelease( keyEvent ) ;
when i use this code in a normal app(whether run as root) , it works fine.
but when i run it in launchd-daemon like the project smjobbless , it's not working.
here is my launch.plist:
<dict>
<key>Label</key>
<string>com.apple.bsd.SMJobBlessHelper</string>
<key>RunAtLoad</key>
<true/>
<key>MachServices</key>
<dict>
<key>com.apple.bsd.SMJobBlessHelper</key>
<true/>
</dict>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
</dict>
the launchd-daemon project had been installed at /Library/PrivilegedHelperTools
success and everything is running OK (seems run as root) except the CGEventPost
, it's very strange , any suggestions?
CodePudding user response:
Daemon processes do not have access to the window session. UI events should be pushed back over the XPC channel to a process in the user's session (generally running as the user rather than root). If you want a launchd configuration, consider LaunchAgents rather than LaunchDaemons.
Consider how this would work under fast user switching when there are multiple active window sessions.
Directly to your question, in the above-linked docs, note:
The Core Graphics framework relies on the presence of the window server and thus is available only to applications running in a login session.
Your daemon does not run in a login session.