Kit Cross

Remapping tilde key on macOS

A recent purchase of a Magic Keyboard (largely due to the infamous butterfly key mechanism) prompted some fiddling because the key layout was missing the tilde key. Otherwise known as the key above the tab key.

For reasons unknown, Apple has chosen to swap ~ with § (a rarely used symbol for sections in a document) on British layouts.

Sierra introduced hidutil to remap key bindings. The tool itself has almost no help information available on the command line, but there is some documentation from Apple. You’ll need the hex code of the key and then to or it with 0x700000000.

hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064},{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}]}'

Problem solved? Not quite. The mapping will be lost as soon as you reboot. Initially I looked at making the above a login hook. macOS has had the ability to run a login script of your choice since 10.2.

At some point this must have been deprecated, because I could not get this to work. I would imagine for security reasons, as runs the login hook as root.

The alternative is to use Automator. To create a self–contained mini-app that sets your key bindings:

  1. Open Automator and create a new project as an application
  2. Drag out “Run Shell Script”
  3. Enter your bindings from above
  4. Export the application to your Applications directory. It isn’t necessary to code sign the bundle

Finally, head to System Preferences > Users & Groups > Login Items and drag your new application in to add it to the list. A headache but quickly done and avoids the need for any third party software.