Skip to content

Instantly share code, notes, and snippets.

@sundeepgupta
Created May 1, 2024 21:11
Show Gist options
  • Save sundeepgupta/5a9b1e9c5dec4916863aade55a7d6aed to your computer and use it in GitHub Desktop.
Save sundeepgupta/5a9b1e9c5dec4916863aade55a7d6aed to your computer and use it in GitHub Desktop.
How to remap keys in macOS Sonoma
  1. Go to https://hidutil-generator.netlify.app/ to create the ~/Library/LaunchAgents/com.local.KeyRemapping.plist file.

Example:

<?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/sudo</string>
        <string>/usr/bin/hidutil</string>
        <string>property</string>
        <string>--set</string>
        <string>{"UserKeyMapping":[
            {
              "HIDKeyboardModifierMappingSrc": 0x700000005,
              "HIDKeyboardModifierMappingDst": 0x7000000E3
            },
            {
              "HIDKeyboardModifierMappingSrc": 0x700000065,
              "HIDKeyboardModifierMappingDst": 0x7000000E6
            }
        ]}</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
  1. Notice the <string>/usr/bin/sudo</string>? Yes, now sudo is needed for certain keys. To make this work automatically, need to add to the sudoers file.

    1. Open it for editing via sudo visudo.
    2. At the bottom add <your_username> ALL = (ALL) NOPASSWD: /usr/bin/hidutil
  2. To manually load and unload the plist file:

    1. launchctl load -w ~/Library/LaunchAgents/com.local.KeyRemapping.plist
    2. launchctl unload ~/Library/LaunchAgents/com.local.KeyRemapping.plist
  3. To manually get and set key remappings:

    1. hidutil property --get "UserKeyMapping"
    2. hidutil property --set '{"UserKeyMapping":[]}' (this clears them)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment