Change Right Modifier Keys on MacOS

Problem: when you switch between Apple Mac and normal keyboards and use German keyboard layout, you can change the keyboard layout on MacOS to “de standard” to have most characters where you would expect them (e.g. @ { } …) but on german keyboards the “AltGr” is located directly right of the space bar. On MacOS the modifier key for AltGr is “Option”. So it happens that you accidentally hit “Command-Q” and close an app instead of getting “@”. There is a settings to switch “Command” and “Option” in MacOS Settings, but this changes “Option” and “Command” keys on both sides. But it is possible to change right “Option” and “Command” key only, via CLI.

Solution: Switch “Option” and “Command” keys in the MacOS settings. Then enter “defaults -currentHost read -g” to see the global default settings. You will find a setting like this:

    "com.apple.keyboard.modifiermapping.1452-835-0" =     (
                {
            HIDKeyboardModifierMappingDst = 30064771299;
            HIDKeyboardModifierMappingSrc = 30064771302;
        },
                {
            HIDKeyboardModifierMappingDst = 30064771303;
            HIDKeyboardModifierMappingSrc = 30064771298;
        },
                {
            HIDKeyboardModifierMappingDst = 30064771298;
            HIDKeyboardModifierMappingSrc = 30064771303;
        },
                {
            HIDKeyboardModifierMappingDst = 30064771302;
            HIDKeyboardModifierMappingSrc = 30064771299;
        }
    );

The first line is the ID of your keyboard on this computer. In my case it’s “….1452-835-0”. The system setting swaps both key pairs. Now make your own settings with:

defaults -currentHost write -g "com.apple.keyboard.modifiermapping.1452-835-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>
"

This swaps the right modifier keys only. You have to logout and login again to activate this settings. I didn’t find a way to reread this settings on the fly.

If you like right command AND right option to function as “AltGr” you can use this command:

defaults -currentHost write -g "com.apple.keyboard.modifiermapping.1452-835-0" -array "
<dict>            
  <key>HIDKeyboardModifierMappingDst</key><integer>30064771302</integer>
  <key>HIDKeyboardModifierMappingSrc</key><integer>30064771303</integer>
</dict>
"

Version: MacOS 12.6.2 Monteray