Remapping Caps Lock in macOS Sierra
As you may recall, I used Karabiner and Seil to remap my Caps Lock previously, allowing me to use it for Alfred or OmniFocus.
However, Karabiner no longer works in macOS Sierra and the alternative Karabiner-Elements has yet to support multiple keystrokes for a single key.
My muscle memory caused some issues on this after upgrading to macOS Sierra, as I've grown kinda used to using Caps Lock for OmniFocus, suddenly requiring me to skip that habit.
Karabiner-Elements?
Karabiner-Elements is a new set of sub commands from the original Karabiner, for now basically a scaffold for the next version of Karabiner.
Even though it doesn't (yet!) support firing multiple keystrokes on a single key, Karabiner-Elements is not all bad, as it allows for rebinding Caps Lock to some F-keys not usually used on the Mac keyboard (Specifically F13-20), which will allow for remapping Caps Lock to fire something which you wouldn't normally use.
After installing Karabiner-Elements, open it up and setup the following:
This gets us part of the way, but not quite. We still need to trigger something on pressing the F18 button.
Hammerspoon to the rescue
I recently discovered Hammerspoon, as it's popped up a few places as being the next big thing for automation geeks on macOS.
Hammerspoon is basically a software-bridge between the lower levels of macOS (using the Accessibility API in macOS) and a Lua scripting engine. This means you can monitor and automate behavior, based on things like WiFi available, screens connected etc. However, it also allows you to do systemwide shortcuts, which is what we'll use here.
In this case, we want to remap F18 into firing a combination of the Option key (alt) and Space, effectively doing what Karabiner-Elements currently cannot.
I won't go into the details on installing Hammerspoon here, they have a pretty decent introduction on the site. Once it's installed, Hammerspoon looks for init.lua
in the .hammerspoon
directory in your home directory, to determine the rules and shortcurts you've set up. To avoid confusion, I won't include all of my init.lua here, but this is basically what you would need to include, in order to allow F18 to fire alt
+ space
. The reason for the additional check on other keys, is to allow for using Caps Lock as an additional modifier key, allowing for more bindings using it, if needed:
-- Enter Hyper Mode when F18 (Caps Lock) is pressed
pressedF18 = function()
k.triggered = false
k:enter()
end
-- Leave Hyper Mode when F18 (Caps Lock) is released,
-- fire alt + space if no other keys are pressed.
releasedF18 = function()
k:exit()
if not k.triggered then
hs.eventtap.keyStroke({"alt"}, 'SPACE')
end
end
-- Bind the Hyper key
f18 = hs.hotkey.bind({}, 'F18', pressedF18, releasedF18)
Once you have the code in your init.lua
, reload Hammerspoon (If it's not set to do that automatically), and you're good to go, Caps Lock should now fire alt
+ space
!
Good luck on hacking your keyboard in macOS Sierra - I guarantee that Hammerspoon will be a rabbithole, if you're into automation ;)