Skip to content

Instantly share code, notes, and snippets.

@lylejantzi3rd
Forked from kbussell/ctrlTap.lua
Created July 11, 2018 01:27
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 lylejantzi3rd/d5e17c41b53922749d3c1b9cd5760dd0 to your computer and use it in GitHub Desktop.
Save lylejantzi3rd/d5e17c41b53922749d3c1b9cd5760dd0 to your computer and use it in GitHub Desktop.
Send escape key if the ctrl key is tapped. (Used along side remapping my Caps Lock key to ctrl) Thanks to @asmagill 's examples for a starting point.
local alert = require("hs.alert")
local timer = require("hs.timer")
local eventtap = require("hs.eventtap")
local events = eventtap.event.types
local module = {}
-- timeout for ctrol key
module.timeFrame = .25
-- what to do when the ctrl key has been tapped
module.action = function()
eventtap.keyStroke({}, 'escape')
-- alert("esc")
end
local ctrlPressed = false
-- Synopsis:
-- what we're looking for is the ctrl key down event followed by
-- the key up within a short time frame with no other key or flag
-- change event before the specified time has passed
-- verify that *only* the ctrl key flag is being pressed
local onlyCtrl = function(ev)
local result = ev:getFlags().ctrl
for k,v in pairs(ev:getFlags()) do
if k ~= "ctrl" and v then
result = false
break
end
end
return result
end
-- verify that no modifier keys are being pressed
local noModifiers = function(ev)
result = true
for k,v in pairs(ev:getFlags()) do
if v == true then
result = false
break
end
end
return result
end
module.eventwatcher = eventtap.new({events.flagsChanged, events.keyDown}, function(ev)
ev_type = ev:getType()
if ev_type == events.flagsChanged then
if onlyCtrl(ev) then
module.countDownTimer = timer.doAfter(module.timeFrame, function()
module.countDownTimer = nil
ctrlPressed = false
end)
ctrlPressed = true
elseif noModifiers(ev) then
if ctrlPressed and module.action then
module.action()
end
else
ctrlPressed = false
end
elseif ev_type == events.keyDown then
ctrlPressed = false
end
return false ;
end):start()
return module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment