Skip to content

Instantly share code, notes, and snippets.

@aibolik
Last active May 30, 2020 07:15
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 aibolik/64bcf27ab9535d52368058ba05e0a028 to your computer and use it in GitHub Desktop.
Save aibolik/64bcf27ab9535d52368058ba05e0a028 to your computer and use it in GitHub Desktop.
Remapping keys on MacOS - own and external keyboard

Remapping keys with hidutil

hidutil is a powerful tool that can remap keys on your MacOS. It can remap keys for internal keyboard as well as for external keyboard. In the example below, I am remapping section sign key(§) to grave accent key(`), because I want to have my recently bought K380 keyboard with UK layout to act almost like US layout.

Note: there are GUI tools like Karabiner, but it messed by FN keys on external keyboard, when I just wanted simple one key remap, so I found this might be useful if you want simple solution.

Arguments and mapping

--set - this one sets your mapping. What is important is HIDKeyboardModifierMappingSrc and HIDKeyboardModifierMappingDst. As their name tells it is mapping from source to destination. You can use this table https://www.freebsddiary.org/APC/usb_hid_usages.php to find the key you need.

Note: first you need to put page number(1st column in the table that is in the link) and then key itself (2nd column) with padded extra zeros.

Example - map A -> B. Both are on page 7, A is 0x04 and B is 0x05, so your mapping would be:

{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000004,"HIDKeyboardModifierMappingDst":0x700000005}]}

Note that it will only map A -> B, not B -> A. If you want B -> A, just add another entry in UserKeyMapping array.

--matching - property targets my specific device. I guess you don't need that if you want to map internal keyboard. ProductID can be found in Apple icon -> System Report -> Bluetooth (or USB depending on how your device is connected) -> Your device and ProductID will be there.

Making it work on logins

I followed instructions here: https://www.nanoant.com/mac/macos-function-key-remapping-with-hidutil, however for some reason it does not work on restarts, but it works when I log out of my user and Log in.

Just put the com.example.KeyRemapping.plist below in your /Library/LaunchAgents or ~/Library/LaunchAgents folder with changed keys. Here is what to change:

  • <string> after <key>Label</key>: it should be unique name for your agent. Convention is to name the file the same as Label(+ extension for filename of course). Another convention is to use reverse domain notation.

  • in <array> after <key>ProgramArguments</key>, you just put the command as you do it for remapping.

Well, that's it. If you find solution how to make it work on reboots, leave a comment and help other people.

Thanks :)

<?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.example.KeyRemapping</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/hidutil</string>
<string>property</string>
<string>--matching</string>
<string>{"ProductID":0x1234}</string>
<string>--set</string>
<string>{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}]}</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
#!/bin/sh
hidutil property --matching '{"ProductID":0x1234}' --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}]}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment