Using a PC keyboard with a Macintosh - key mapping
Yes, you can do some of this through the OS X / macOS built-in Control Panel (Keyboard -> Modifier Keys... and swap the Command and Option keys), but that doesn’t address the Windows keyboard “menu” key or things like media keys. For that we need to remap the keys using Karabiner (13.x for Catalina or Big Sur; 12.x for Sierra through Catalina; 11.x for El Capitan, 10.x for Mavericks through El Capitan; if you’re still using a “cat” operating system, you might be SOL).
Alternate Method
Using hdiutil to remap keys
- Keyboard LeftAlt (0xE2) to Keyboard Left GUI (a/k/a the Command ⌘ key) (0xE3)
- Keyboard Keyboard Left GUI (0xE3) to LeftAlt (0xE2)
- Keyboard RightAlt (0xE6) to Keyboard Right GUI (0xE7)
- Keyboard Application key (0x65) to Keyboard Right Alt (0xE6)
#!/usr/bin/env bash
hidutil property --set '{
"UserKeyMapping":[
{
"HIDKeyboardModifierMappingSrc":0x7000000E2,
"HIDKeyboardModifierMappingDst":0x7000000E3
},
{
"HIDKeyboardModifierMappingSrc":0x7000000E3,
"HIDKeyboardModifierMappingDst":0x7000000E2
},
{
"HIDKeyboardModifierMappingSrc":0x7000000E6,
"HIDKeyboardModifierMappingDst":0x7000000E7
},
{
"HIDKeyboardModifierMappingSrc":0x700000065,
"HIDKeyboardModifierMappingDst":0x7000000E6
}
]
}'
Make persistent with LaunchAgent
<?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>Label</key>
<string>com.local.KeyRemapping</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/hidutil</string>
<string>property</string>
<string>--set</string>
<string>{"UserKeyMapping":[
{
"HIDKeyboardModifierMappingSrc":0x7000000E2,
"HIDKeyboardModifierMappingDst":0x7000000E3
},
{
"HIDKeyboardModifierMappingSrc":0x7000000E3,
"HIDKeyboardModifierMappingDst":0x7000000E2
},
{
"HIDKeyboardModifierMappingSrc":0x7000000E6,
"HIDKeyboardModifierMappingDst":0x7000000E7
},
{
"HIDKeyboardModifierMappingSrc":0x700000065,
"HIDKeyboardModifierMappingDst":0x7000000E6
}
]}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Notes:
- I’m not aware at this time of any way to make the mapping specific to a particular keyboard; remapped keys will be remapped no matter which keyboard you’re using (i.e., the built-in keyboard on a MacBook or the external Wintel board you’ve hooked up). So you’ll have just swapped Cmd and Option on the built in keyboard. You can alias an “unmap” command (see note 2 below), or just go into the Keyboard control panel’s Modifier Keys screen and swap them back for the internal keyboard. (Though admittedly that’s all kind of a kludge...)
- If you really bork things, you can wipe out all the remapping you’ve done by running this command in the terminal: hidutil property --set '{"UserKeyMapping":[]}'
- You can check the state of any keymaps (e.g., after a reboot, to see if the LaunchAgent setup above worked) using the following command (void or a set of empty parantheses () indicates no keys are remapped): hidutil property --get "UserKeyMapping"
Comments
Post a Comment