Last active
November 24, 2020 23:01
-
-
Save amirrajan/ace70cd22eeb1f0ff4c1ff3758ebb7d1 to your computer and use it in GitHub Desktop.
Hammerspoon script so that ctrl acts as esc if it's pressed by itself
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
--- ========================================== | |
--- Reload this config if it changes | |
--- ========================================== | |
function reloadConfig(files) | |
doReload = false | |
for _,file in pairs(files) do | |
if file:sub(-4) == ".lua" then | |
doReload = true | |
end | |
end | |
if doReload then | |
result, success = hs.execute("luac -p ~/.hammerspoon/init.lua 2>&1 >/dev/null", true) | |
if not success then | |
hs.alert(result) | |
return | |
end | |
hs.reload() | |
end | |
end | |
hs_config_watcher = hs.pathwatcher.new(os.getenv("HOME") .. "/.hammerspoon/", reloadConfig):start() | |
hs.alert.show("Config loaded.") | |
g_mod_keys = true | |
-- ========================================== | |
-- ESC | |
-- ========================================== | |
send_escape = false | |
last_mods = {} | |
control_key_handler = function() | |
send_escape = false | |
end | |
control_key_timer = hs.timer.delayed.new(0.15, control_key_handler) | |
control_handler = function(evt) | |
if not g_mod_keys then | |
return | |
end | |
local new_mods = evt:getFlags() | |
if last_mods["ctrl"] == new_mods["ctrl"] then | |
return false | |
end | |
if not last_mods["ctrl"] then | |
last_mods = new_mods | |
send_escape = true | |
control_key_timer:start() | |
else | |
if send_escape then | |
if not g_hs_mode then | |
hs.eventtap.event.newKeyEvent({}, 'escape', true):post() | |
hs.eventtap.event.newKeyEvent({}, 'escape', false):post() | |
end | |
end | |
last_mods = new_mods | |
control_key_timer:stop() | |
end | |
return false | |
end | |
control_tap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, control_handler) | |
control_tap:start() | |
other_handler = function(evt) | |
send_escape = false | |
return false | |
end | |
other_tap = hs.eventtap.new({hs.eventtap.event.types.keyDown}, other_handler) | |
other_tap:start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment