Skip to content

Instantly share code, notes, and snippets.

@arleighdickerson
Last active November 28, 2016 17:10
Show Gist options
  • Save arleighdickerson/a253316b95da2c0d808037c2208686e3 to your computer and use it in GitHub Desktop.
Save arleighdickerson/a253316b95da2c0d808037c2208686e3 to your computer and use it in GitHub Desktop.
init.lua for hammerspoon... tap caps lock for escape, hold for ctrl, shift and caps lock tap sends shift escape
-- adapted from https://gist.github.com/arbelt/b91e1f38a0880afb316dd5b5732759f1
-- if shift key is held during a caps lock tap, send shift-escape
ctrl_table = {
send_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")
if last_mods["shift"] then
ctrl_table["send_shift"] = true
end
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
local sendWith = {};
-- log.i("send escape key...")
if last_mods["shift"] then
sendWith = {"shift"}
end
hs.eventtap.keyStroke(sendWith, "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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment