To remap a key, you can use the hidutil
command. In this example, I am remapping F16 to the ⌥ Option on the right side (probably not used much, if at all).
hidutil property --set '{"UserKeyMapping": \
[{\"HIDKeyboardModifierMappingSrc":0x7000000E6, \
"HIDKeyboardModifierMappingDst":0x70000006A}]}}
The backslashes (\) allow for a multi-line command to facilitate better readability. The command can be copied/pasted as-is or the backslashes and new-lines removed to make it a true one-liner.
When you're writing the command, keep this in mind:
- Src = the key you want to press
- Dst = what you want it to do
I have supplied a portion of the Hex values for the extended function keys as defined in Apple Technical Note TN240 that describes how to use hidutil
.
Key |
Hex Code |
Key |
Hex Code |
Keyboard F13 |
0x68 |
Keyboard F19 |
0x6E |
Keyboard F14 |
0x69 |
Keyboard F20 |
0x6F |
Keyboard F15 |
0x6A |
Keyboard F21 |
0x70 |
Keyboard F16 |
0x6B |
Keyboard F22 |
0x71 |
Keyboard F17 |
0x6C |
Keyboard F23 |
0x72 |
Keyboard F18 |
0x6D |
Keyboard F24 |
0x73 |
Improving with a script...
To make this complex command easier to work with, I have created a simple Bash script (below) that will allow you to specify the source and destination keys as variables.
I used a function, mdCMDStr
, to "make the command string" so you can easily replicate additional commands with minimal effort. Simply, reassign the SOURCE
and DEST
variables and call the mkCMDStr
and hidutil
commands as many times as necessary.
#! /bin/bash
function mkCMDStr () {
# Creates the command string
SETKEY_CMD={\"UserKeyMapping\":[{\"HIDKeyboardModifierMappingSrc\":${1},\"HIDKeyboardModifierMappingDst\":${2}}]}
}
SOURCE=0x7000000E6 #Keyboard Right Option/Alt
DEST=0x70000006A #Keyboard F16
mkCMDStr $SOURCE $DEST
hidutil property --set "${SETKEY_CMD}"
exit
Download the Script
This will not persist through reboots. You can make this "permanent" by putting this script in Login Items or creating a LaunchAgent.