Skip to content

Instantly share code, notes, and snippets.

@spinscale
Created April 16, 2017 18:19
Show Gist options
  • Select an option

  • Save spinscale/fd82f00da29447990f27f36b3f4b927d to your computer and use it in GitHub Desktop.

Select an option

Save spinscale/fd82f00da29447990f27f36b3f4b927d to your computer and use it in GitHub Desktop.
Hammerspoon: Configure volume via keyboard shortcuts for macbook pro (touchbar, but no keys)
function changeVolume(diff)
return function()
local current = hs.audiodevice.defaultOutputDevice():volume()
local new = math.min(100, math.max(0, math.floor(current + diff)))
if new > 0 then
hs.audiodevice.defaultOutputDevice():setMuted(false)
end
hs.alert.closeAll(0.0)
hs.alert.show("Volume " .. new .. "%", {}, 0.5)
hs.audiodevice.defaultOutputDevice():setVolume(new)
end
end
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'Down', changeVolume(-3))
hs.hotkey.bind({'ctrl', 'alt', 'cmd'}, 'Up', changeVolume(3))
@cameljava

Copy link
Copy Markdown

excellent work, thanks, mate. It works like a charm.

@cameljava

Copy link
Copy Markdown

Hi, Alexander:

Not sure if you can see this message, I have tried your lua, it seem only works when I click volume icon on menu bar, if I use those key combination randomly anywhere, it doesn't work.

Have you got this issue?

Thanks,
Kevin

@spinscale

Copy link
Copy Markdown
Author

works for me under 10.15.4, with hammerspoon 0.9.78

@spinscale

Copy link
Copy Markdown
Author

every volume change also displays a popup for me... can you bind the keys to do sth else just for testing? or play around in the hammerspoon console?

@cameljava

Copy link
Copy Markdown

Thanks so much Alexander for getting back to me so quickly.
I am on same mac and hammerspoon versions. very new on lua, not sure how to display debug msg in lua.
The wired part is, if I have click volumn icon on menu bar, volumn list is displayed, your hotkey works properly.

But I am in other application, use the hotkey, will not affect volumn at all

ghost commented May 11, 2021

Copy link
Copy Markdown

Thanks so much

@zhangkai803

Copy link
Copy Markdown

works perfectly on my mechine, macOS 13.3.1, Hammerspoon 0.9.100.

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