macoskeyboardpreferencescapslocksystem-preferences

Does anyone know where OSX stores the settings in System Preferences > Keyboard > Modifier Keys?


Where are these stored?!?

I'm apparently not the only one who wants to know (How can I change modifier keys in "System Preferences > Keyboard > Modifier Keys..."). I've tried watching the System Preferences app with dtruss, but that doesn't seem to be possible on 10.10.3 (which is what I'm on right now), and I'm not even sure that that would be at all useful if System Preferences is just getting settings from cfprefsd. Watching cfprefsd with dtruss doesn't seem to catch the relevant file access.

Does anyone know of an API to get this information? Something in gestalt perhaps?


Solution

  • Ok - answering my own question. I threw together a little program that uses kqueues to watch file system changes. I watched my file system for modifications when I changed the setting in System Preferences, and I saw:

    '/Users/ted/Library/Preferences/ByHost/.GlobalPreferences.3F1C...9C34.plist.V1Ut9hp' kevent: ident=44, filter=KQ_FILTER_VNODE, flags=KQ_EV_ADD|KQ_EV_CLEAR, fflags=KQ_NOTE_WRITE|KQ_NOTE_CHILD|KQ_NOTE_PDATAMASK
    

    So the setting is in ~/Library/Preferences/ByHost/.GlobalPreferences.<UUID>.plist. I'm not sure what the UUID is - something do to with OpenDirectory? (UPDATE: apparently this UUID is an identifier for your machine).

    Anyways, in .GlobalPreferences.<UUID>.plist, we see entries like:

    <key>com.apple.keyboard.modifiermapping.1452-610-0</key>
    <array>
        <dict>
            <key>HIDKeyboardModifierMappingDst</key>
            <integer>2</integer>
            <key>HIDKeyboardModifierMappingSrc</key>
            <integer>0</integer>
        </dict>
    </array>
    

    1452:610 is the decimal VID:PID for the USB keyboard/trackpad combo in my 2012 Macbook Pro, and any other keyboard that's been plugged in to my computer and has had modifier settings remapped will have a similar entry. 0 seems to be the Caps Lock key, 2 and 10 seem to be left and right CTRL, 3 and 11 seem to be left and right Option, and 4 and 12 seem to be left and right Command.

    So from the above, you can see that I have my Caps Lock key mapped to left CTRL.

    It looks like the settings in .GlobalPreferences.<UUID>.plist are a part of your standard user defaults. So you can get at these settings easily enough just by doing

    [[NSUserDefaults standardUserDefaults] objectForKey:@"com.apple.keyboard.modifiermapping-1452-610-0"];