Skip to content

Instantly share code, notes, and snippets.

@mysteriouss
Last active August 2, 2022 18:26
Embed
What would you like to do?
M1 MacBook Air F5 & F6 KeyRemap
#!/bin/sh

## xref: https://www.reddit.com/r/MacOS/comments/jy5ry8/m1_macbook_air_keysshortcut_for_keyboard/ghryk3j/?utm_source=reddit&utm_medium=web2x&context=3

cat > ~/Library/LaunchAgents/com.local.KeyRemapping.plist << EOF
<?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": 0xC000000CF,
              "HIDKeyboardModifierMappingDst": 0xFF00000009
            },
            {
              "HIDKeyboardModifierMappingSrc": 0x10000009B,
              "HIDKeyboardModifierMappingDst": 0xFF00000008
            }
        ]}</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
EOF

launchctl load ~/Library/LaunchAgents/com.local.KeyRemapping.plist

Unload:

launchctl unload ~/Library/LaunchAgents/com.local.KeyRemapping.plist
rm -f ~/Library/LaunchAgents/com.local.KeyRemapping.plist
@pirate
Copy link

pirate commented Aug 2, 2022

Thanks for this!

Do you know what keycodes I should use to remap the Search button + voice button instead of the Do Not Disturb button?

Edit: found it

/* AppleVendor Keyboard Page (0xff01) */
enum {
    kHIDUsage_AppleVendorKeyboard_Spotlight             = 0x0001,
    kHIDUsage_AppleVendorKeyboard_Dashboard             = 0x0002,
    kHIDUsage_AppleVendorKeyboard_Function              = 0x0003,
    kHIDUsage_AppleVendorKeyboard_Launchpad             = 0x0004,
    kHIDUsage_AppleVendorKeyboard_Reserved              = 0x000A,
    kHIDUsage_AppleVendorKeyboard_CapsLockDelayEnable   = 0x000B,
    kHIDUsage_AppleVendorKeyboard_PowerState            = 0x000C,
    kHIDUsage_AppleVendorKeyboard_Expose_All            = 0x0010,
    kHIDUsage_AppleVendorKeyboard_Expose_Desktop        = 0x0011,
    kHIDUsage_AppleVendorKeyboard_Brightness_Up         = 0x0020,
    kHIDUsage_AppleVendorKeyboard_Brightness_Down       = 0x0021,
    kHIDUsage_AppleVendorKeyboard_Language              = 0x0030,
};

So for example, for

https://mybyways.com/blog/script-to-re-map-macbook-pro-function-keys

  • 0xC000000CF = do not disturb
  • 0x10000009B = dictation
  • 0x0C00000221 = spotlight
  • 0xFF0100000010 = mission control
  • 0x0C000002A2 = launchpad

Found from here: reddit thread, HID spec hut1_22.pdf


Another source has these:

/* AppleVendor Keyboard Page (0xff01) */
enum {
    kHIDUsage_AppleVendorKeyboard_Spotlight             = 0x0001,
    kHIDUsage_AppleVendorKeyboard_Dashboard             = 0x0002,
    kHIDUsage_AppleVendorKeyboard_Function              = 0x0003,
    kHIDUsage_AppleVendorKeyboard_Launchpad             = 0x0004,
    kHIDUsage_AppleVendorKeyboard_Reserved              = 0x000A,
    kHIDUsage_AppleVendorKeyboard_CapsLockDelayEnable   = 0x000B,
    kHIDUsage_AppleVendorKeyboard_PowerState            = 0x000C,
    kHIDUsage_AppleVendorKeyboard_Expose_All            = 0x0010,
    kHIDUsage_AppleVendorKeyboard_Expose_Desktop        = 0x0011,
    kHIDUsage_AppleVendorKeyboard_Brightness_Up         = 0x0020,
    kHIDUsage_AppleVendorKeyboard_Brightness_Down       = 0x0021,
    kHIDUsage_AppleVendorKeyboard_Language              = 0x0030,
};

https://mybyways.com/blog/script-to-re-map-macbook-pro-function-keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment