Hammerspoon / hammerspoon

Staggeringly powerful macOS desktop automation with Lua
http://www.hammerspoon.org
MIT License
12.02k stars 583 forks source link

Binding action to caps lock key? #510

Closed lord closed 9 years ago

lord commented 9 years ago

Is it possible to bind a function to a caps lock key press? (It'd be cool to not have to install an entire different program to just rebind caps lock) Doesn't look like there's a capslock...I've also tried created an eventtap to listen for caps lock key presses, but they don't seem to appear for modifier keys. I guess I could poll the new checkKeyboardModifiers but that seems pretty sub-optimal and it also means the caps lock key would continue to operate normally.

(Also, thanks for creating this project, it's pretty incredible!)

cmsj commented 9 years ago

@lord sadly not, CapsLock is interpreted too far down in the bowels of OS X for us to intercept directly.

I'm not aware of anything other than Karabiner that can do this.

lord commented 9 years ago

Ah, gotcha. Thanks for the fast response!

cmsj commented 9 years ago

@lord FWIW, my setup has Karabiner turn CapsLock into KeyCode::COMMAND_R, ModifierFlag::OPTION_R | ModifierFlag::SHIFT_R | ModifierFlag::CONTROL_R | ModifierFlag::NONE

I then do local hyper = {"⌘", "⌥", "⌃", "⇧"} in my ~/.hammerspoon/init.lua and map all my hotkeys with that.

Almost all :)

I have a few which use Karabiner to combine my fake Hyper key with the _L variants of the keyboard modifiers, allowing me to do things like shift-hyper/cmd-hyper. These don't work as hotkeys in Hammerspoon because there's no way to propagate up the "both cmd keys are being held" information, instead they are dispatched as URLs to hs.urlevent. It's a bit weird, but very cool.

There are some details of how to make the URL events work, at http://www.hammerspoon.org/go/#karabinerurl

lord commented 9 years ago

Haha, super elaborate! I just 5 minutes ago managed to get what I wanted to work using Seil to rebind caps lock to F16 and then just binding my hammerspoon function to that — the goal was to use caps lock as a key to switch between windows in the current space. :smile:

cmsj commented 9 years ago

:)

Zhichao-Hong commented 9 years ago

@cmsj Can you please share your Karabiner private.xml on how you are binding Hyper with _L keys? I tried without any success.

cmsj commented 9 years ago

@Zhichao-Hong First up, I remap Caps Lock in this way: http://www.tenshu.net/p/fake-hyper-key-for-osx.html - note that my Hyper uses the _R variants of all the modifier keys.

To do stuff like hyperfnleft I have Karabiner emit hammerspoon:// URLs rather than keycodes. There's a basic intro do using Karabiner and Hammerspoon URLs at http://www.hammerspoon.org/go/#karabinerurl

My Karabiner private.xml is: https://gist.github.com/cmsj/e365fbba43d76f9e3f6f My hammerspoon init.lua is: https://gist.github.com/cmsj/986489fff6f3c1b96771

Hope that helps!

Zhichao-Hong commented 9 years ago

@cmsj: Thank you for the gists. And they worked perfectly. I originally also maps CAPS LOCK to ESCAPE when tapped. Unfortunately, this is no longer an option with your private.xml. Do you know if I still can achieve this with your settings?

cmsj commented 9 years ago

@Zhichao-Hong it ought to be possible, just create extra mappings for whatever physical key combinations you want in Karabiner, and have them emit URLs to Hammerspoon

Zhichao-Hong commented 9 years ago

@cmsj Thank you. I actually turned SHIFT_L and SHIFT_R to ESCAPE key when they are tapped. Worked much better now. Thank you so much for this wonderful app and config!

franciscolourenco commented 7 years ago

@cmsj what to do now that karabiner is not supported in Sierra?

bsingr commented 7 years ago

@aristidesfl instead of seil/karabiner I now use karabiner-elements in sierra: https://github.com/tekezo/Karabiner-Elements

franciscolourenco commented 7 years ago

@bsingr I know, but elements doesn't support a big part of the features of karabiner.. :/ specifically homerow mode and tab mode

kevinforrestconnors commented 2 years ago

@lord sadly not, CapsLock is interpreted too far down in the bowels of OS X for us to intercept directly.

I'm not aware of anything other than Karabiner that can do this.

I can't install Karabiner on my work computer, is there any other way to implement hyper?

jubr commented 1 year ago

@kevinforrestconnors the free https://hyperkey.app does the trick - on the same ⛵ here.

Oh & you can prevent the Caps-Lock functionality from being left activated after using the hotkey-combo using hidutil with something along the lines of:

hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000073,"Comment":"Caps-Lock to F24"}]}'

Google it a bit for more options and how to make it persistent using launchctl.