Skip to content

Instantly share code, notes, and snippets.

@hexcowboy
Last active November 27, 2024 22:17
Show Gist options
  • Save hexcowboy/f25b95238a60f3d9e2c97de86e22d7bb to your computer and use it in GitHub Desktop.
Save hexcowboy/f25b95238a60f3d9e2c97de86e22d7bb to your computer and use it in GitHub Desktop.
macOS Remap Keys

Finding your keyboard location

You can typically find the location ID of your keyboard with this command

hidutil list | grep -i keyboard | awk '{print $3}' | sort -u | grep -v 0x0

Replace this part of the bash command to your own keyboard location:

<string>{"LocationID": 0xa5}</string>
# https://developer.apple.com/library/archive/technotes/tn2450/_index.html#//apple_ref/doc/uid/DTS40017618-CH1-KEY_TABLE_USAGES
# Caps: 0x700000039
# Tab: 0x70000002B
# Esc: 0x700000029
# RCmd: 0x7000000E7
# LCtl: 0x7000000E0
hidutil property --matching '{"LocationID": 0xa5}' --set '{
"UserKeyMapping": [
{
"HIDKeyboardModifierMappingSrc": 0x700000039,
"HIDKeyboardModifierMappingDst": 0x70000002B
},
{
"HIDKeyboardModifierMappingSrc": 0x70000002B,
"HIDKeyboardModifierMappingDst": 0x700000029
},
{
"HIDKeyboardModifierMappingSrc": 0x7000000E7,
"HIDKeyboardModifierMappingDst": 0x7000000E0
}
]
}'
# View the current keybinds: hidutil property --get "UserKeyMapping"
# The above script will not persist between reboots. Instead, use this to save your keybindings between sessions.
# https://rakhesh.com/mac/using-hidutil-to-map-macos-keyboard-keys/
# Use https://hidutil-generator.netlify.app/ to generate your own
# View the current keybinds: hidutil property --get "UserKeyMapping"
mkdir -p ~/Library/LaunchAgents
echo '
<?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>--matching</string>
<string>{"LocationID": 0xa5}</string>
<string>--set</string>
<string>{"UserKeyMapping":[
{
"HIDKeyboardModifierMappingSrc": 0x700000039,
"HIDKeyboardModifierMappingDst": 0x70000002B
},
{
"HIDKeyboardModifierMappingSrc": 0x70000002B,
"HIDKeyboardModifierMappingDst": 0x700000029
},
{
"HIDKeyboardModifierMappingSrc": 0x7000000E7,
"HIDKeyboardModifierMappingDst": 0x7000000E0
}
]}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
' >> ~/Library/LaunchAgents/com.local.KeyRemapping.plist
echo 'success'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment