Pétur Pétur - 4 months ago 6x
Objective-C Question

How can I change modifier keys in "System Preferences > Keyboard > Modifier Keys..."

I need to programmatically change the values of the Caps Lock, Control, Option and Command keys in "System Preferences > Keyboard > Modifier Keys..."

I dont want to use AppleScript.

Could someone point me in the right direction?


Here is apple script capsLockOff.scpt:

tell application "System Preferences"
    set current pane to pane "com.apple.preference.keyboard"
end tell

tell application "System Events"

    tell application process "System Preferences"
        get properties

        click button "Modifier Keys…" of tab group 1 of window "Keyboard"
        tell sheet 1 of window "Keyboard"
            click pop up button 4
            click menu item "No Action" of menu 1 of pop up button 4
            delay 1
            click button "OK"

        end tell
    end tell
end tell

tell application "System Preferences" to quit

Following is cocoa code to call above script. Hope this will help


    NSString *fileName = @"capsLockOff";

    NSString* path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"scpt"];
    NSURL* url = [NSURL fileURLWithPath:path];NSDictionary* errors = [NSDictionary dictionary];
    NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
    [appleScript executeAndReturnError:nil];
    [appleScript release];