MacOS – Updating modifier keys from the command line takes no effect

command linedefaultskeybindingskeyboardmacos

The goal: map "Caps Lock" to "Control" from the command-line.

I'm trying to update the modifier keys from the command-line with the following command as explained here and here but to no effect.

$ defaults -currentHost write -g "com.apple.keyboard.modifiermapping.1452-611-0" '(
{
  HIDKeyboardModifierMappingDst = 2;
  HIDKeyboardModifierMappingSrc = 0;
})'

When I do it from the "System preferences" it works right away and seem to be configured the same as when I do it the manually, as seen below:

$ defaults -currentHost read -g "com.apple.keyboard.modifiermapping.1452-611-0"
(
        {
        HIDKeyboardModifierMappingDst = 2;
        HIDKeyboardModifierMappingSrc = 0;
    }
)

This is the same exact result as when running it manually or from the GUI.

When restarting after running the command line, I see that in "System preferences" it's set correctly, like I expect but, it doesn't work. It only works when resetting do default and setting it manually.

I even tried copying the plist file as suggested here again to no effect (even with restart).

Next I tried this with no success (ideas from here):

defaults -currentHost write -globalDomain <rest of command>
defaults write NSGlobalDomain <rest of command>
defaults write GlobalPreferences <rest of command>

Running OSX 10.9.4 (Mavrics).

Any ideas are welcome.

Best Answer

It's because values are written as strings not integers. You can see that using:

$ plutil -convert xml1 -o - ~/Library/Preferences/ByHost/.GlobalPreferences.*.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.keyboard.modifiermapping.1452-591-0</key>
    <array>
        <dict>
            <key>HIDKeyboardModifierMappingDst</key>
            <string>30064771303</string>
            <key>HIDKeyboardModifierMappingSrc</key>
            <string>30064771302</string>
        </dict>
        <dict>
            <key>HIDKeyboardModifierMappingDst</key>
            <string>30064771302</string>
            <key>HIDKeyboardModifierMappingSrc</key>
            <string>30064771303</string>
        </dict>
    </array>
</dict>
</plist>

So the right way to update the setting is using XML format:

$ defaults -currentHost write -g com.apple.keyboard.modifiermapping.1452-591-0 -array \
'<dict>
    <key>HIDKeyboardModifierMappingDst</key>
    <integer>30064771303</integer>
    <key>HIDKeyboardModifierMappingSrc</key>
    <integer>30064771302</integer>
</dict>' \
'<dict>
    <key>HIDKeyboardModifierMappingDst</key>
    <integer>30064771302</integer>
    <key>HIDKeyboardModifierMappingSrc</key>
    <integer>30064771303</integer>
</dict>'