Skip to content

Instantly share code, notes, and snippets.

@johnzweng
Last active November 13, 2020 06:32
Show Gist options
  • Save johnzweng/49e94ccb19a8dca164667e7fad574561 to your computer and use it in GitHub Desktop.
Save johnzweng/49e94ccb19a8dca164667e7fad574561 to your computer and use it in GitHub Desktop.
Remap key codes on macOS
#!/usr/bin/env bash
#
# Remap some keys on macOS (>=Sierra)
#
# Johnny, 2.11.2020
#
# Sources: based on https://gist.github.com/zbstof/6cba7d54e105cc5148c8a943d1581cad
#
# Find Key codes:
# https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-247/IOHIDFamily/IOHIDUsageTables.h
# https://developer.apple.com/library/archive/technotes/tn2450/_index.html
# To automatically run at each startup:
# sudo defaults write com.apple.loginwindow LoginHook /path/to/this/file/remap_keys.sh
#
# Note: there can be only 1 LoginHook script, so you may check before if already one exists:
# sudo defaults read com.apple.loginwindow LoginHook
# (see also: https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CustomLogin.html)
#
# To delete the LoginHook entry later again, just call:
# sudo defaults delete com.apple.loginwindow LoginHook /path/to/this/file/remap_keys.sh
CAPS_LOCK="0x700000039"
F19=0x70000006E
# Some other keys (see links above for more)
#ESCAPE="0x700000029"
#NON_US_BACKSLASH_PLUS_MINUS="0x700000064"
#GRAVE_ACCENT_AND_TILDE="0x700000035"
#SLASH_PIPE="0x700000031"
#ENTER_RETURN="0x700000028"
#RIGHT_SHIFT="0x7000000E5" #Do nothing button
FROM="\"HIDKeyboardModifierMappingSrc\""
TO="\"HIDKeyboardModifierMappingDst\""
#
# You can add as many UserKeyMapping's as you want
# in the 'UserKeyMapping' JSON-array below:
#
hidutil property --set "{\"UserKeyMapping\":[
{ $FROM: $CAPS_LOCK, $TO: $F19 }
]}"
#
# If you want to manually check the current usermapping:
#
# hidutil property --get "UserKeyMapping"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment