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:
- The current process is running as the root user.
- Access for assistive devices is enabled. In Mac OS X v10.4 & later, you can enable this feature using System Preferences, Universal Access panel, Keyboard view.
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
Ok y'all the solution / some background info can be found at this address..
So, Apple came up with another solution in Tiger that solves these problems: the magic function AXMakeProcessTrusted. That will enable the API just for your application and needs to be called from a process running as root, so it is secure. It is also all automatic, so aside from asking the user for his/her password, there is nothing the user needs to do. The problem is that it seems no one uses it. Every third party application I have seen and even Automator just asks the user to manually check the box in System Preferences. It is more work to implement and has one huge undocumented bug (the application must be relaunched before it is actually trusted Update: reported as #5711990), but I really think people should be using it. So, I thought I would release the code to make it easy to implement it in your app. It includes a helper agent that you should be able to just drop into your project.
It's usually considered rude to change a user's system setting without at least telling them, if not explicitly asking for permission. Most apps that need this setting just check to see if it's enable and if not, tell the user to turn it on.
There is a way to enable it on a per-process basis. Unfortunately i dont know what it is, i have apps that do this tho and i think i recall seeing something about it on the cocoa dev mailing list
Are you sure that it's possible to enable it on a per-process basis, without needing to have admin rights? And if yes, will all events be captures, or only the ones belonging to the process?
精彩评论