Björn Brynjúlfur

Remapping keys on macOS

I have now used the new M1 MacBook Air for a month and am very happy with it overall. It is really fast, stays cool all the time, there is no fan noise, the battery lasts for ages the new keyboard feels great to type on.

The only thing I haven't liked is Apple's decision to change the function keys. In particular, they replaced the keyboard brightness adjustment buttons with new buttons for Spotlight, Dictation and Do Not Disturb. I don't use any of these features and have missed the ability to change they keyboard brightness.

The most common use case is when I watch a TV show or a movie. When I open up VLC or Netflix, I realize that the keyboard backlight distracts my eyes. Previously, I could turn the keyboard backlight off with the function keys, but now I have to use the trackpad and a few clicks, which takes me out of full-screen mode.

Luckily, after Googling for a while, I found the solution on Reddit. Since macOS 10.12 Sierra, Apple has included a tool called hidutil, which allows user to remap keyboard keys.

Following the instructions in one of the comments on the Reddit thread, I was able to remap the F5 and F6 function keys (Dictation and Do Not Disturb) back to the keyboard backlight adjustment.

The steps are:

  1. Create a file called "com.local.KeyRemapping.plist" in "~/Library/LaunchAgents"
  2. Add the following code:
<?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>
  1. Reboot to run the new LaunchAgent file.

After doing this, the F5 and F6 function keys now adjust the keyboard brightness, just like before.

Limitations

I wanted to take this further, namely by also remapping the F3 and F4 function keys. They are by default assigned to Workspaces and Spotlight, which I never use. Instead, I would like the F3 function key to toggle dark/light mode, and the F4 function key to toggle True Tone. I toggle dark/light mode in the mornings and evenings, and I turn off True Tone when watching a TV show or a film.

Unfortunately, I wasn't able to do this. The hex codes for remapping these two particular keys are not provided by Apple in their hidutil documentation, and Googling didn't provide me with the hex codes either.

As a workaround, I now use Alfred workflows to bind F3 to toggle light/dark mode, and F4 to toggle True Tone. This has two downsides:

  1. I need to hold the fn key to run these workflows, which is inconvenient
  2. The True Tone toggle is done via a script which opens up System Preferences, taking me out of full-screen mode.

macOS is surprisingly inflexible in some situations, including this one. Hopefully more ambitious users will solve this in the near future, but for now this works well enough that I will not put more effort into this.

Newer: Moving from Goodreads to my own website

Older: Enjoying front-end design with Tailwind