39

I want to remap § to ` and ± to ~ on my Mac keyboard because they are located in a place I am not used to. Is there an easy way to do this?

bmike
  • 235,889
Mahdi
  • 573

4 Answers4

49

Remapping § to ` and ± to ~ worked on my Mac (running OS X 10.15.6) without additional software with the following code snippet.

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

To do this automatically at startup - Create a new file named ~/Library/LaunchAgents/com.user.loginscript.plist

with the following content:

<?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.user.loginscript</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/hidutil</string>
        <string>property</string>
        <string>--set</string>
        <string>{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000035, "HIDKeyboardModifierMappingDst":0x700000064}, {"HIDKeyboardModifierMappingSrc":0x700000064, "HIDKeyboardModifierMappingDst":0x700000035}]}</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

The file needs to be registered with a one-off execution of the following command:

launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist

See also this solution which explains how to use Automator instead of launchctl.

  • to get the hex codes of keys: https://apps.apple.com/tr/app/key-codes/id414568915?l=tr&mt=12 – Gabriel Petersson Mar 22 '22 at 17:49
  • In my case - I didn't had to run launchctl load - also that command returned errors Load failed: 5: Input/output error – Gal Bracha Mar 24 '22 at 10:10
  • 1
    Is it possible to make it work only for Macbook keybaord? I have an external keybaord that the characters are correctly mapped, and switching between the keybaords can be a pain sometimes – farzadshbfn May 11 '22 at 10:40
  • You can also get the key codes at: https://developer.apple.com/library/archive/technotes/tn2450/_index.html

    Also, you can use Automator to get the script to load on startup: https://www.quora.com/How-do-you-know-if-your-startup-will-be-successful/answer/Jordi-Noguer?srid=kCsL

    – Francis Huang Jun 19 '22 at 20:32
  • 3
    Still works ! Big thanks for this trick – Jean-Bernard Jansen Aug 12 '22 at 13:33
  • @farzadshbfn thats weird, in my case the external keyboard backtik would input § on the mac so I do not have to switch – Jean-Bernard Jansen Aug 12 '22 at 13:40
  • @jean-bernardJansen That’s exactly what I meant. I have uk layout macbook, and us layout keyboard… I wanted it swapped for macbook, but not for keyboard. If you have a uk keyboard as well, I think that’s expected – farzadshbfn Aug 13 '22 at 14:40
  • @farzadshbfn I meant I need the switch for both macbook and external keyboard since the backtick sends the same code than what the mac has for §. So I need the remap for both. It is somehow a US qwerty (a charachorder CC1 to be accurate) – Jean-Bernard Jansen Sep 01 '22 at 12:41
  • Omg amazing! This was the only two keys that I wanted to remap on the international ABC keyboard! Thanks – tarikki Sep 15 '22 at 05:41
  • 2
    @farzadshbfn Yes, you can apply the remapping to a specific keyboard. Add the following lines inside the ... block:
        <string>property</string>
        <string>--matching</string>
        <string>{"ProductID":0x343}</string>
    
    

    Where 0x343 should be replaced with the ProductID of your internal macbook keyboard. To find the correct ProductID on your macbook, click: Apple icon > About This Mac > System Report > Hardware > SPI > Apple Internal Keyboard

    See also DarthRitis's answer: https://stackoverflow.com/a/58981641/56879

    – Amit Moscovich Jan 02 '23 at 12:59
  • 1
    Works as it is expected on macOS 13 Ventura. Thanks! – pa4080 Mar 14 '23 at 11:52
19

The two apps most commonly used to do this are

Ukelele

and

Karabiner

There’s a lot of other options, but these are the easiest in practice for most people to just manage their layouts easily.

bmike
  • 235,889
Tom Gewecke
  • 20,864
  • Found it here: https://github.com/tekezo/Karabiner-Elements/blob/master/usage/README.md#how-to-configure-karabiner-elements – Mahdi Apr 20 '17 at 16:49
  • There's also the possibility that OP has selected the wrong keyboard layout on the input menu. – WGroleau Sep 30 '20 at 17:25
  • @WGroleau I've not seen any alternative input source that would do that. Perhaps you know of one? This exchange is typical of someone shifting from a US ANSI English keyboard, where `/~ is on the topmost leftmost key, to a European English ISO keyboard, where it moves to the bottom leftmost key. Or vice versa. – Tom Gewecke Sep 30 '20 at 17:59
  • Well, you just mentioned two keyboards that relocate the key OP is concerned with. – WGroleau Sep 30 '20 at 18:09
  • @WGroleau You are confusing hardware with software. ANSI and ISO are differnet kinds of hardware keyboard, not keyboard layouts, and you can't select them in the input menu. Any given keyboard layout you select in the input menu will look slightly different depending on which kind of hardware you have attached, because ISO has one key more than ANSI. – Tom Gewecke Sep 30 '20 at 20:11
  • I'm well aware of the difference. I said keyboard layout and shortened it the second time. OP is concerned with remapping, not changing hardware. Remapping is what keyboard layout files do. It may be there is no keyboard layout that puts those glyphs on that key. I haven't examine all of Apple's dozens of layouts. – WGroleau Oct 01 '20 at 01:11
6

https://github.com/tekezo/Karabiner-Elements/blob/master/usage/README.md#how-to-configure-karabiner-elements

Here is explains "EXACTLY" the problem that I am having with a simple solution.

You need to update karabiner.json and add this part:

{
    "profiles": [
        {
            "name": "Default profile",
            "selected": true,
            "simple_modifications": {
                "non_us_backslash": "grave_accent_and_tilde",
                "grave_accent_and_tilde" : "non_us_backslash"
            }
        }
    ]
}
Mahdi
  • 573
1

After MacOS 14.3, hidutil now requires admin privileges to run certain remaps and if you want to run it automatically it will not work the same way the first answer did.

To resolve this issue I had to do it this way.

Create a new script to any folder you want.

#!/bin/zsh
sudo hidutil property --set '{"UserKeyMapping":
    [{"HIDKeyboardModifierMappingSrc":0x700000035,
      "HIDKeyboardModifierMappingDst":0x700000064},
     {"HIDKeyboardModifierMappingSrc":0x700000064,
      "HIDKeyboardModifierMappingDst":0x700000035}]
}'

Create a file under

/Library/LaunchDaemons/com.yourusername.remapkeys.plist

<?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.yourusername.remapkeys</string>
    <key>RunAtLoad</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/zsh</string>
        <string>/path/to/your/script/com.yourusername.remapkeys.sh</string>
    </array>
</dict>
</plist>

Set permissions

sudo chown root:wheel /Library/LaunchDaemons/com.yourusername.remapkeys.plist
sudo chmod 644 /Library/LaunchDaemons/com.yourusername.remapkeys.plist

Load the daemon

sudo launchctl load -w /Library/LaunchDaemons/com.yourusername.remapkeys.plist

Restart and test

Raffi
  • 111