Created
October 2, 2016 20:55
-
-
Save arbelt/b91e1f38a0880afb316dd5b5732759f1 to your computer and use it in GitHub Desktop.
Hammerspoon config to send escape on short ctrl press
This file contains hidden or 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
ctrl_table = { | |
sends_escape = true, | |
last_mods = {} | |
} | |
control_key_timer = hs.timer.delayed.new(0.15, function() | |
ctrl_table["send_escape"] = false | |
-- log.i("timer fired") | |
-- control_key_timer:stop() | |
end | |
) | |
last_mods = {} | |
control_handler = function(evt) | |
local new_mods = evt:getFlags() | |
if last_mods["ctrl"] == new_mods["ctrl"] then | |
return false | |
end | |
if not last_mods["ctrl"] then | |
-- log.i("control pressed") | |
last_mods = new_mods | |
ctrl_table["send_escape"] = true | |
-- log.i("starting timer") | |
control_key_timer:start() | |
else | |
-- log.i("contrtol released") | |
-- log.i(ctrl_table["send_escape"]) | |
if ctrl_table["send_escape"] then | |
-- log.i("send escape key...") | |
hs.eventtap.keyStroke({}, "ESCAPE") | |
end | |
last_mods = new_mods | |
control_key_timer:stop() | |
end | |
return false | |
end | |
control_tap = hs.eventtap.new({12}, control_handler) | |
control_tap:start() | |
@safecancel you can grab the current active application and use that to determine if the logic should run.
Thanks everyone. I've been using Karabiner-Elements only to remap caps lock to escape on tap and control on hold. With your solutions, I can use Hammerspoon instead (after changing caps lock to control in System Preferences) and drop Karabiner as a dependency.
This is a great and very helpful gist. I used it to get my application to more reliably trigger the ESCAPE key. Since sending it with the hs.eventtap.sendKey
does not seem to reliably do so (especially in applications like Alfred, 1Password, and such.
But the tip by @wezzynl in their comment above got me thinking and it actually does work much faster and more reliable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi everyone,
Is there a way to not have this run when a specific app is in focus? (for example: remote desktop, vmware, teamviewer etc.)
Many thanks!