I want to enable Access for assistive devices in System Preferences programmatically. But Problem is that my application is not running as root user and i do not want my application to be as root user and also should not ask for any authentication in between.
I want to tap all keyboard events globally. I am using CGEventTapCreate() for the same.In the documentation of CGEventTapCreate() API it is mentioned that, Event taps receive key up and key down events if one of the following conditions is true:
I tried manually by checking the Enable Access for assistive devices from System Preference and it gives me expected output.
So is there any way to do the same via program without asking for authentication and also application is not running as root user?
Thanks,
Dheeraj.
You can run an Applescript (or translate the Applescript into ScriptingBridge or whatever your Objective-C layer over AppleEvents is)
Here is an Applescript that I use in one particular project that does something similar to what you need:
on isUIScriptingOn()
tell application "System Events" to set isUIScriptingEnabled to UI elements enabled
return isUIScriptingEnabled
end isUIScriptingOn
on turnUIScriptingOn(switch)
tell application "System Events"
activate
set UI elements enabled to switch
end tell
end turnUIScriptingOn
on run
if not isUIScriptingOn() then
display dialog "Enable Access for assistive devices (found in the Universal Access System Preference) must be on for this software to correctly work. This program will enable this setting for you"
turnUIScriptingOn(true)
display dialog "Access for assistive devices in now on"
end if
end run