Hamerspoon config examples for hyper key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- hattip https://github.com/lodestone/hyper-hacks | |
-- hattip https://gist.github.com/ttscoff/cce98a711b5476166792d5e6f1ac5907 | |
-- hattip https://gist.github.com/prenagha/1c28f71cb4d52b3133a4bff1b3849c3e | |
-- A global variable for the Hyper Mode | |
k = hs.hotkey.modal.new({}, "F17") | |
-- The following keys are configured as hot keys in their respective apps (or in Keyboard Maestro) | |
-- d → Dash (configure in Dash preferences) | |
-- h → HazeOver (configure in HazeOver preferences) | |
-- m → Moom (configure in Moom preferences) | |
-- n → Notifications configure in System preferences → Keyboard → Shortcuts → Mission Control) | |
-- f → Fantastical (configure in Fantastical preferences) | |
-- t → Typinator (configure in Typinator preferences) | |
-- SPACE → Spotlight (configure in System Preferences → Keyboard → Shortcuts → Spotlight, moved so that ⌘␣ could be used for Quicksilver) | |
hyperBindings = {'d','h','m','n','f','t','SPACE'} | |
for i,key in ipairs(hyperBindings) do | |
k:bind({}, key, nil, function() hs.eventtap.keyStroke({'cmd','alt','shift','ctrl'}, key) | |
k.triggered = true | |
end) | |
end | |
-- Enter Hyper Mode when F18 (Hyper/Capslock) is pressed | |
pressedF18 = function() | |
k.triggered = false | |
k:enter() | |
end | |
-- Leave Hyper Mode when F18 (Hyper/Capslock) is pressed, | |
-- send ESCAPE if no other keys are pressed. | |
releasedF18 = function() | |
k:exit() | |
if not k.triggered then | |
hs.eventtap.keyStroke({}, 'ESCAPE') | |
end | |
end | |
-- Bind the Hyper key | |
f18 = hs.hotkey.bind({}, 'F18', pressedF18, releasedF18) | |
-- AppleScript bindings | |
k:bind({}, 'o', nil, function() hs.execute ('osascript /Users/manthei/Dropbox/AppleScripts/OmniFocus/QuickOpen.scpt') k.triggered = true end) | |
hs.hotkey.bind({'ctrl', 'alt'}, 'SPACE', function () hs.execute ('osascript /Users/manthei/Dropbox/AppleScripts/OmniFocus/OopsieFocus.scpt') end) | |
-- USB events | |
local usbWatcher = nil | |
function usbDeviceCallback(data) | |
if (data["productName"] == "ScanSnap S1500M") then | |
if (data["eventType"] == "added") then | |
hs.application.launchOrFocus("ScanSnap Manager") | |
elseif (data["eventType"] == "removed") then | |
app = hs.appfinder.appFromName("ScanSnap Manager") | |
app:kill() | |
end | |
end | |
end | |
usbWatcher = hs.usb.watcher.new(usbDeviceCallback) | |
usbWatcher:start() | |
local volumeBeep = hs.sound.getByName('Pop') | |
local volumeText | |
local volumeTextTimer | |
function hideVolumeText() | |
if volumeText then | |
volumeText:hide(0.1) | |
end | |
end | |
-- hattip https://www.snip2code.com/Snippet/1378996/A-hammerspoon-snippet-that-lets-you-hype/ | |
hs.eventtap.new({hs.eventtap.event.types.scrollWheel}, function(e) | |
local mods = hs.eventtap.checkKeyboardModifiers() | |
if mods.alt and mods.ctrl and mods.shift then | |
delta = e:getProperty( | |
hs.eventtap.event.properties.scrollWheelEventDeltaAxis1) | |
hs.audiodevice.defaultOutputDevice():setVolume( | |
hs.audiodevice.defaultOutputDevice():volume() - delta | |
) | |
local newVolume = | |
math.floor(hs.audiodevice.defaultOutputDevice():volume()) | |
local speakerEmoji | |
if newVolume > 60 then | |
speakerEmoji = '🔊' | |
elseif newVolume > 40 then | |
speakerEmoji = '🔉' | |
elseif newVolume > 0 then | |
speakerEmoji = '🔈' | |
else | |
speakerEmoji = '🔇' | |
end | |
if not volumeText then | |
volumeText = hs.drawing.text(hs.geometry.rect(20, 35, 200, 200), '') | |
volumeText:setLevel(hs.drawing.windowLevels.cursor + 1) | |
volumeText:setTextColor({1, 0, 0}) | |
end | |
volumeText:setText(speakerEmoji .. ' ' .. tostring(newVolume) .. '%') | |
volumeText:show() | |
if not volumeTextTimer then | |
volumeTextTimer = hs.timer.delayed.new(1, hideVolumeText) | |
end | |
volumeTextTimer:start() | |
return true | |
end | |
end):start() | |
-- local alert_sound = hs.sound.getByFile("alert.wav") | |
local alert_sound = hs.sound.getByName("Tink") | |
alert_sound:play() | |
hs.alert.show("Hammerspoon, at your service.", 2) |
This is the simplest and clearest hammerspoon example for Hyper key I could find. I really appreciate you posting. My muscle memory thanks you.
Followed this from Brett's comment. Thanks for posting there. Like @todkon noted, this showed how not to be stuck in hyper mode, etc. Really let it be clear how to let keys pass through to applications.
How would you adapt this to a simple "Press capslock with another key and send l_control or if you release capslock w/o hitting another key send escape"? I'm looking at this section and can't quite put it together.
-- Enter Hyper Mode when F18 (Hyper/Capslock) is pressed
pressedF18 = function()
k.triggered = false
k:enter()
end
-- Leave Hyper Mode when F18 (Hyper/Capslock) is pressed,
-- send ESCAPE if no other keys are pressed.
releasedF18 = function()
k:exit()
if not k.triggered then
hs.eventtap.keyStroke({}, 'ESCAPE')
end
end
-- Bind the Hyper key
f18 = hs.hotkey.bind({}, 'F18', pressedF18, releasedF18)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! Thanks a whole bunch! Was getting stumped on setting Hammerspoon up by not having it stuck in hyper mode. This was exactly what I've been looking for. Cheers!