From 46123783335aab2f40c89b684d5add8d2618cb7d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Thu, 20 Jun 2019 00:53:30 +0200 Subject: application_switch: Provide access to F1–F4 keys Since I overrode the F1 through F4 keys globally, there was no way to access press a non-custom variant of these keys. Realised this when trying to access the manual in Mutt, which is bound to F1. Add Shift-F1–F4 bindings that map to F1–F4, in case we need access to the original set of keys. Needed to add a `doAfter` delay after disabling the binding because the delay seems to take a bit to go through. Thanks to 'knu' (https://github.com/knu) for the tip: https://github.com/Hammerspoon/hammerspoon/issues/1252#issuecomment-290411701 Also gleaned it from: https://stackoverflow.com/questions/40986242/key-repeats-are-delayed-in-my-hammerspoon-script --- application_switch.lua | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'application_switch.lua') diff --git a/application_switch.lua b/application_switch.lua index 84dbe88..988a2b6 100644 --- a/application_switch.lua +++ b/application_switch.lua @@ -1,21 +1,37 @@ local activateAllWindows = true -hs.hotkey.bind({}, 'f1', function() +application_switch = {} + +application_switch['f1'] = hs.hotkey.bind({}, 'f1', function() hs.application.find('com.apple.Terminal'):activate() end) -hs.hotkey.bind({}, 'f2', function() +application_switch['f2'] = hs.hotkey.bind({}, 'f2', function() hs.application.find('org.mozilla.nightly'):activate(activateAllWindows) end) -hs.hotkey.bind({}, 'f3', function() +application_switch['f3'] = hs.hotkey.bind({}, 'f3', function() hs.application.find('com.google.Chrome'):activate(activateAllWindows) end) -hs.hotkey.bind({}, 'f4', function() +application_switch['f4'] = hs.hotkey.bind({}, 'f4', function() hs.application.find('com.microsoft.rdc.osx.beta'):activate() end) +-- Rebind Shift-F[n] to F[n] +for _, key in ipairs({'f1', 'f2', 'f3', 'f4'}) do + hs.hotkey.bind({'shift'}, key, function() + application_switch[key]:disable() + + hs.timer.doAfter(0.05, function() + hs.eventtap.keyStroke({}, key) + + application_switch[key]:enable() + end) + end) +end + + -- Mode to activate lesser-used applications application_switch_mode = hs.hotkey.modal.new({}, 'f13') application_switch_mode:bind({}, 'f13', function() -- cgit v1.2.3