views:

185

answers:

3

Hi All,

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:

  1. The current process is running as the root user.
  2. 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.

+2  A: 

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
RyanWilcox
+1  A: 

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.

jshier
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
mustISignUp
A: 

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?

Cristian