Skip to content

Instantly share code, notes, and snippets.

@maurofaccenda
Last active March 8, 2025 08:53
Show Gist options
  • Save maurofaccenda/aca92d695220e545c5e7ffd28dc365c4 to your computer and use it in GitHub Desktop.
Save maurofaccenda/aca92d695220e545c5e7ffd28dc365c4 to your computer and use it in GitHub Desktop.
hs.hotkey.bind({}, "§", function() hs.eventtap.keyStroke({}, "`") end )
hs.hotkey.bind({ "cmd" }, "§", function() hs.eventtap.keyStroke({ "cmd" }, "`") end )
hs.hotkey.bind({ "shift" }, "§", function() hs.eventtap.keyStroke({ "shift" }, "`") end )
hs.hotkey.bind({ "cmd", "shift" }, "§", function() hs.eventtap.keyStroke({ "cmd", "shift" }, "`") end )
@maurofaccenda
Copy link
Author

maurofaccenda commented Jan 9, 2024

Intro

For those with a Mac with English ISO keyboard and would like to use the § key as backtick (`). Specially after MacOS 14.2 update when the hidutil command stopped working.

How to use it

  1. Install Hammerspoon:
$ brew install hammerspoon
  1. Give Hammerspoon Accessibility permission
    System Settings -> Privacy & Security -> Accessibility

  2. Configure Hammerspoon to Launch at login
    Open Hammerspoon, go to Preferences and check the box on Launch Hammerspoon at login

  3. Create the file on ~/.hammerspoon/init.lua with the content above

  4. Click Reload config on Hammerspoon Console window (or system tray icon).

hidtool is fixed in macOS 14.3 (Sonoma)

However, it needs sudo now. To achieve the same results as the above use the following command:

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

hidtool doesn't require sudo in macOS 15.2 (Sequoia)

It doesn't require sudo anymore, however, it is required to provide "Input Monitoring" permission to the terminal application (I use iTerm).

Note: I did not try with macOS 15 or 15.1

@ghigt
Copy link

ghigt commented Jan 15, 2024

Thank you for the advice, I works well, but not for "~" (shift + §). I can't figure out how to do it (hammerspoon doesn't accept "~" character in its config). Did you find a way to solve it?

@maurofaccenda
Copy link
Author

Hi @ghigt , not sure what you meant. But shift + § = ± and I wanted it to behave like shift + backtick which produces ~ and it works fine. If you want it to enter another char, you can just replace the key combination on the keystroke part.

Remember to always use the key modifier and the "lowercase" key (eg. if you want ( use hs.eventtap.keyStroke({ "shift" }, "9"))

@ghigt
Copy link

ghigt commented Jan 15, 2024

I exactly want the same behavior you describe (shift + backtick producing ~). It doesn't works and still print ±.

It looks like hs.hotkey.bind({ "shift" }, "§", function() hs.eventtap.keyStroke({ "shift" }, "`") end ) doesn't behave well for me.

@maurofaccenda
Copy link
Author

maurofaccenda commented Jan 15, 2024

@ghigt I am sorry to hear that. It works fine for me. You can also play with some debugging and see what's going on.
I used this (paste on the console and once you are done, just reload config) it might help identify the problem:

local function keyCallback(event)
    hs.alert(hs.keycodes.map[event:getKeyCode()])
end

keyWatcher = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, keyCallback)
keyWatcher:start()

As for reference, after pasting that on the console and pressing shift+§ I get 2 notifications: one with § and another with backtick.

@ghigt
Copy link

ghigt commented Jan 15, 2024

After testing with your debug, I finally saw that the ~ worked out of the MacOS Terminal app (it works fine with iTerm though). So I don't know why but it works everywhere except in the app I want to use it 😄

Big thanks for your help!!

@VojtechVitek
Copy link

The sudo hdutil stopped working for me as of today (macos 15.2) with the following error:

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

Attempt to remap alphanumerics / special characters. If setting fails, ensure Terminal has input monitoring permissions.

This Hammerspoon config works and saved my day! Thank you! 🎉

@ghigt
Copy link

ghigt commented Jan 26, 2025

@VojtechVitek hdutil shouldn't need sudo after MacOS 15.2 update any more. Did you try without?

@VojtechVitek
Copy link

$ hidutil property --set '{"UserKeyMapping": [{"HIDKeyboardModifierMappingSrc": 0x700000064, "HIDKeyboardModifierMappingDst": 0x700000035}]}'
Attempt to remap alphanumerics / special characters. If setting fails, ensure Terminal has input monitoring permissions.

@ghigt
Copy link

ghigt commented Jan 27, 2025

@maurofaccenda
Copy link
Author

maurofaccenda commented Jan 28, 2025

My current MacBook has the American keyboard layout, so I don't need this anymore, however, it is important to keep this updated for those who need it.

Thank you @ghigt and @VojtechVitek. I updated the first comment to reflect your findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment