Skip to content

Instantly share code, notes, and snippets.

@tommorris
Created March 8, 2023 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommorris/a7b26cffb2b43cd527a01c3babbbf3d4 to your computer and use it in GitHub Desktop.
Save tommorris/a7b26cffb2b43cd527a01c3babbbf3d4 to your computer and use it in GitHub Desktop.
Hammerspoon rules for binding pedals
require('util')
pedal_rules = {
l = {
{
name = "Google Meet (show chat)",
predicate = isAppRunning("Google Chrome"),
action = hs.fnutils.partial(dispatchCommandToChrome, {'ctrl', 'cmd'}, 'c'),
},
{
name = "Firefox (prev tab)",
predicate = isAppFront("Firefox"),
action = hs.fnutils.partial(hs.eventtap.keyStroke, {'shift', 'cmd'}, '['),
},
{
name = "Fallback",
predicate = true,
action = hs.fnutils.partial(hs.eventtap.keyStroke, {}, 'a'),
},
},
m = {
{
name = "Google Meet (mute/unmute)",
predicate = isAppRunning("Google Chrome"),
action = hs.fnutils.partial(dispatchCommandToChrome, {'cmd'}, 'd'),
},
{
name = "Firefox (next tab)",
predicate = isAppFront("Firefox"),
action = hs.fnutils.partial(hs.eventtap.keyStroke, {'cmd', 'shift'}, ']'),
},
{
name = "Fallback",
predicate = true,
action = hs.fnutils.partial(hs.eventtap.keyStroke, {}, 'b'),
},
},
r = {
{
name = "Google Meet (camera on/off)",
predicate = isAppRunning("Google Chrome"),
action = hs.fnutils.partial(dispatchCommandToChrome, {'ctrl', 'cmd'}, 'e'),
},
{
name = "Firefox (close tab)",
predicate = isAppFront("Firefox"),
action = hs.fnutils.partial(hs.eventtap.keyStroke, {'cmd'}, 'w'),
},
{
name = "Fallback",
predicate = true,
action = hs.fnutils.partial(hs.eventtap.keyStroke, {}, 'c'),
},
},
}
pedal_config = {
l = 'F16',
m = 'F17',
r = 'F18',
}
bindPedals(pedal_config, pedal_rules)
function isAppRunning(appName)
return (function() return hs.application.find(appName) ~= nil end)
end
function isAppFront(appName)
return (function() return hs.application.frontmostApplication():name() == appName end)
end
function dispatchCommandToChrome(modifier, key)
-- return false if one can't, otherwise true
if (isAppRunning("Google Chrome")()) then
if (hs.window.focusedWindow():application():name() ~= "Google Chrome") then
hs.application.launchOrFocus("Google Chrome")
end
hs.eventtap.keyStroke(modifier, key)
else
return false
end
end
function bindPedals(config, rules)
-- config takes table that maps l, m and r to keystrokes
-- rules takes a table containing l, m, r and then a list-like
-- table of rules to be executed sequentially
--
-- each rule is a table containing:
-- name - just for personal reference
-- predicate - function or value to decide if rule should run
-- action - what the rule should do
for pedal, mapping in pairs(config) do
hs.hotkey.bind({}, mapping, function()
for _, rule in pairs(rules[pedal]) do
if type(rule.predicate) == "function" then
if rule.predicate() then
rule.action()
break
end
else
if rule.predicate ~= false then
rule.action()
break
end
end
end
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment