5

I'm using a MacBook Pro with a Swedish physical keyboard, but with U.S. layout defined in System Prefs. However the tilde (~) and plus-minus (±) keys seem switched. The following key combination should produce a ~:

enter image description here

Because the mapping in System Prefs says so:

enter image description here

But it results in a ± symbol.

It seems these two keys (± and ~) are switched somehow. How do I make them work as shown in System Prefs?

  • 1
    The 2nd picture appears to be showing some kind of hybrid between US ANSI & UK ISO layouts. US keyboards have no key to the left of Z, UK keyboards do. Tilde is to the left of Z on UK, but left of 1 on US. You might be better using a UK map instead. – Tetsujin Jul 1 '18 at 8:50
  • @Tetsujin you're right. But switching to British makes no difference, the keys are still swapped. – Conor Taylor Jul 1 '18 at 9:19
  • 1
    Ah, ok, darnit. I'm out of ideas, sorry. – Tetsujin Jul 1 '18 at 9:22
  • 1
    The 2nd picture is in fact the US ISO layout, what you are supposed to get when US is used on an ISO keyboard with the extra key. Aren't the keys also swapped when you are NOT holding down SHIFT? – Tom Gewecke Jul 1 '18 at 9:33
  • 1
    Here is a similar question with a couple answers: apple.stackexchange.com/questions/239395/and-keys-are-swapped – Tom Gewecke Jul 1 '18 at 9:40
7

Here’s a solution that does not use any external tools.

Run this in the Terminal.app:

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

Now try your ~ and ± keys: they should be switched around.

Problem is, this fix will only work until the next restart. To make it permanent, you have to auto-run it on each system load.

You can do that in three Terminal.app commands:

  1. cat << 'EOF' > ~/.tilde-switch && chmod +x ~/.tilde-switch
    hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064},{"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035}]}'
    EOF
    

    (This is all a single command.) This stores the script from above in an invisible executable file in your home directory, ~/.tilde-switch.

  2. sudo /usr/bin/env bash -c "cat > /Library/LaunchDaemons/org.custom.tilde-switch.plist" << EOF
    <?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>org.custom.tilde-switch</string>
        <key>Program</key>
        <string>${HOME}/.tilde-switch</string>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
      </dict>
    </plist>
    EOF
    

    (This is all a single command.) This creates a system task: ‘run the file from step 1 on every startup.’

  3. sudo launchctl load -w -- /Library/LaunchDaemons/org.custom.tilde-switch.plist
    

    This loads (activates) the task from step 2.

Steps 2 and 3 will prompt for your password. And now, your ~ and ± keys are permanently switched.

Note 1

This only works for MacBook’s built-in physical keyboard. Any external keyboards will remain unaffected.

Note 2

To undo the switching script (not the three steps), here’s the reverse script:

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

Note 3

To undo the three steps:

sudo launchctl unload -w -- /Library/LaunchDaemons/org.custom.tilde-switch.plist; sudo rm -f -- /Library/LaunchDaemons/org.custom.tilde-switch.plist ~/.tilde-switch

Credit

This solution is inspired by this article:

http://homeowmorphism.com/articles/17/Remap-CapsLock-Backspace-Sierra

  • Finally! You saved my day – phaberest May 7 at 22:04
  • This solution works perfectly for my built-in keyboard, however, it unfortunately also affects my Ergodox-EZ, where the key i've mapped to grave now produces § – Benny Powers Aug 27 at 4:33
  • I tried this script, but it didn't work as expected ``` #!/bin/bash hidutil property \ --matching '{"Transport":"SPI"}' \ --set ' { "UserKeyMapping": [ {"HIDKeyboardModifierMappingSrc":0x700000035,"HIDKeyboardModifierMappingDst":0x700000064}, {"HIDKeyboardModifierMappingSrc":0x700000064,"HIDKeyboardModifierMappingDst":0x700000035} ] } ' ``` – Benny Powers Aug 27 at 4:56
  • @BennyPowers Interesting. When I tested this with an external keyboard, it remained unaffected. Sorry to say, I am not familiar with how Ergodox-EZ hooks up into the system, perhaps the reason lies there. – CBlew Aug 27 at 7:25
2

Here is Apple documentation with table containing all the keys and corresponding hexes. https://developer.apple.com/library/archive/technotes/tn2450/_index.html

Initial state is no active key remappings:

hidutil property --get UserKeyMapping
(null)

Remap ~ to §± one way:

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

Swap them both ways:

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

For removing any remappings back to initial just pass an empty array:

hidutil property --set '{"UserKeyMapping":[]}'
0

Using Karabiner Elements, you can add a "Simple Modification" as follows:

"Karabiner Elements Preferences" window, "Simple Modifications" tab, "Target Device" dropdown is set to "Apple Internal Keyboard / Trackpad (No manufacturer name)". Two rows are shown in the configuration table. The first row contains "From key": "non_us_backslash", "To key": "grave_accent_and_tilde (<code>)". The second row contains "From key": "grave_accent_and_tilde (</code>)", "To key": "non_us_backslash".

Basically, you need to know that the name of the § (±) key in Karabiner's configuration screen is non_us_backslash

In my case, I used this method specifically for the built-in keyboard, leaving my external keyboards unchanged.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .