MacOS – Changing right-hand Command & Alt key order to be like a Windows keyboard

keyboardmacos

I would like to switch the Right Command (⌘) Key order with the Alt Key under the System Preferences (to be just like Windows) but there appears to be no option under System Preferences.

Does anyone know how to move these keys' locations using Karabiner-Elements or some other software? In this case we would swap the right-hand Alt and Command keys.

Modifier Keys in System Preferences
(source: tekrevue.com)

Best Answer

Apple's Technical Note TN2450 describes how to remap keys. It is important to know that Right Command is also Right GUI. Running the following command will switch Right Command and Right Alt (if you also want to do the Left Command and Left Alt, refer to the technical note to get the hex values and the Python code below to do the or operation).

hidutil property --set '{"UserKeyMapping":
    [{"HIDKeyboardModifierMappingSrc":0x7000000e7,
      "HIDKeyboardModifierMappingDst":0x7000000e6},
     {"HIDKeyboardModifierMappingSrc":0x7000000e6,
      "HIDKeyboardModifierMappingDst":0x7000000e7}]
}'

The table at the bottom of the Technical Note has a list of hex values for each key. To generalize the above answer to switch any keys (at least is macOS Sierra), you must or the hex value from that list together with 0x700000000. The following Python code demonstrates one way to do this.

In [1]: def convert(val):
   ...:     int_val = int(val, 16)
   ...:     ref = '0x700000000'
   ...:     int_ref = int(ref, 16)
   ...:
   ...:     return hex(int_ref | int_val)
   ...:

In [2]: r_alt = '0xE6'

In [3]: print(convert(r_alt))
0x7000000e6