Skip to content

Instantly share code, notes, and snippets.

@andelink
Created February 28, 2025 08:32
Show Gist options
  • Save andelink/9df452c8cafb6790f196277705c45ef1 to your computer and use it in GitHub Desktop.
Save andelink/9df452c8cafb6790f196277705c45ef1 to your computer and use it in GitHub Desktop.
Remapping keys with `hidutil` on MacOS

Remapping keys with hidutil

My keyboards fn key is on the right side rather than the left, and my mac isn’t registering my right-fn key presses at all, so I basically don’t have a fn key. I hardly use my right-ctrl key, so I remapped it to fn with hidutil:

# Get current key mapping
$ hidutil property --get "UserKeyMapping"
(null)

# Remap right_control --> fn
$ hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc": 0x7000000E4, "HIDKeyboardModifierMappingDst": 0xFF00000003}]}'

# Now we see a populated mapping
$ hidutil property --get "UserKeyMapping"
(
        {
        HIDKeyboardModifierMappingDst = 1095216660483;
        HIDKeyboardModifierMappingSrc = 30064771300;
    }
)

# You can clear any mappings with:
$ hidutil property --set '{"UserKeyMapping":[]}'

And now I have a working fn key 🎉

Persisting it through reboots

The above won't last through a reboot, however. To make it stick, I created a launch agent that runs at load. See attached plist definition.

# Load definition into launchd
$ launchctl bootstrap gui/${UID}/ ~/Library/LaunchAgents/com.${USER}.reMapRightCtrl

# Turn it on
$ launchctl enable gui/${UID}/com.${USER}.reMapRightCtrl

# Run it once, immediately
$ launchctl kickstart gui/${UID}/com.${USER}.reMapRightCtrl

Finding the key codes

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