Skip to content

Instantly share code, notes, and snippets.

@matthewadams
Created July 9, 2020 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewadams/9e758139d0400738513ae9997f901b39 to your computer and use it in GitHub Desktop.
Save matthewadams/9e758139d0400738513ae9997f901b39 to your computer and use it in GitHub Desktop.
macOS HID remap script
#!/usr/bin/env bash
usage() {
cat <<EOF
usage:
$0 -p productId remappings
options:
-p,--product-id: productId The keyboard product id (from Menu Bar\\⌥\SystemInformation...\Hardware)
remappings: one or more colon-separated source/destination HID keyboard/keypad ID pairs; if restoring mapping, you may omit the colon & second ID
Example: swap left-gui to left-option & right-gui to right-option
$0 -p 0x0817 E2:E3 E3:E2 E6:E7 E7:E6
Example: restore mapping above
$0 -p 0x0817 E2 E3 E6 E7
EOF
}
if echo "$@" | grep -q help; then
usage
exit 0
fi
shift
HIDUTIL_PRODUCT_ID=$1
if [ -z "$HIDUTIL_PRODUCT_ID" ]; then
usage
exit 1
fi
shift
if [ -z "$*" ]; then # no mappings
exit 0
fi
sep=
mappings='{"UserKeyMapping":['
for mapping in $@; do
sd=(${mapping//:/ })
src=${sd[0]}
dst=${sd[1]}
if [ -z "$src" ]; then
usage
exit 2
fi
if [ -z "$dst" ]; then
dst=$src
fi
src="$(echo -n $src | tr '[:lower:]' '[:upper:]')"
dst="$(echo -n $dst | tr '[:lower:]' '[:upper:]')"
mappings="$mappings$sep{\"HIDKeyboardModifierMappingSrc\":0x7000000$src,\"HIDKeyboardModifierMappingDst\":0x7000000$dst}"
if [ -z "$sep" ]; then
sep=,
fi
done
mappings="$mappings]}"
hidutil property --matching "{\"ProductID\":$HIDUTIL_PRODUCT_ID}" --set "$mappings"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment