This spoon addresses a limitation within the hs.hotkey module that allows the creation of hotkeys bound to specific left or right keyboard modifiers while leaving the other side free.
This is accomplished by creating unactivated hotkeys for each definition and using an hs.eventtap watcher to detect when modifier keys are pressed and conditionally activating only those hotkeys which correspond to the left or right modifiers currently active as specified by the bind
and new
methods of this spoon.
The LeftRightHotkeyObject
that is returned by LeftRightHotkey:new and LeftRightHotkey:bind supports the following methods in a manner similar to the hs.hotkey equivalents:
LeftRightHotkeyObject:enable()
-- enables the registered hotkey.LeftRightHotkeyObject:disable()
-- disables the registered hotkey.LeftRightHotkeyObject:delete()
-- deletes the registered hotkey.The following modifiers are recognized by this spoon in the modifier table when setting up hotkeys with this spoon:
The modifiers table for any given hotkey is all inclusive; this means that if you specify { "rShift", "lShift" }
then both the left and right shift keys must be pressed to trigger the hotkey -- if you want either/or, then stick with hs.hotkey.
Alternatively, if you want to setup a hotkey when either command key is pressed with only the right shift, you would need to set up two hotkeys with this spoon:
e.g. LeftRightHotkey:bind({ "rCmd", "rShift" }, "a", myFunction)
and LeftRightHotkey:bind({ "lCmd", "rShift" }, "a", myFunction)
This spoon works by using an eventtap to detect flag changes (modifier keys) and when they change, the appropriate hotkeys are enabled or disabled. This means that you should be aware of the following:
hs.hotkey:enable()
-- this means that in truth, either side's modifiers would trigger the callback. Under normal circumstances this won't be noticed because as soon as you switch to the alternate side's modifier, the flag change event will be detected and the hotkey will be disabled. However, as noted above, if Hammerspoon is particularly busy, it is possible for this event to be missed.Like all Spoons, don't forget to use the LeftRightHotkey:start() method to activate the modifier key watcher.
Download: https://github.com/Hammerspoon/Spoons/raw/master/Spoons/LeftRightHotkey.spoon.zip
Signature | LeftRightHotkey:bind(mods, key, [message,] pressedfn, releasedfn, repeatfn) -> LeftRightHotkeyObject |
---|---|
Type | Method |
Description | Create and enable a new hotkey with the specified left/right specific modifiers. |
Parameters |
|
Returns |
|
Notes |
|
Source | Source/LeftRightHotkey.spoon/init.lua line 415 |
Signature | LeftRightHotkey:deleteAll(mods, key) |
---|---|
Type | Method |
Description | Deletes all previously set callbacks for a given keyboard combination |
Parameters |
|
Returns |
|
Source | Source/LeftRightHotkey.spoon/init.lua line 326 |
Signature | LeftRightHotkey:disableAll(mods, key) |
---|---|
Type | Method |
Description | Disables all previously set callbacks for a given keyboard combination |
Parameters |
|
Returns |
|
Source | Source/LeftRightHotkey.spoon/init.lua line 373 |
Signature | LeftRightHotkey:new(mods, key, [message,] pressedfn, releasedfn, repeatfn) -> LeftRightHotkeyObject |
---|---|
Type | Method |
Description | Create a new hotkey with the specified left/right specific modifiers. |
Parameters |
|
Returns |
|
Notes |
|
Source | Source/LeftRightHotkey.spoon/init.lua line 263 |
Signature | LeftRightHotkey:start() -> self |
---|---|
Type | Method |
Description | Starts watching for flag (modifier key) change events that can determine if the right or left modifiers have been pressed. |
Parameters |
|
Returns |
|
Notes |
|
Source | Source/LeftRightHotkey.spoon/init.lua line 444 |
Signature | LeftRightHotkey:stop() -> self |
---|---|
Type | Method |
Description | Stops watching for flag (modifier key) change events that can determine if the right or left modifiers have been pressed. |
Parameters |
|
Returns |
|
Notes |
|
Source | Source/LeftRightHotkey.spoon/init.lua line 471 |