dotfiles/wm/hammerspoon/ctrlDoubleTap.lua

71 lines
1.4 KiB
Lua
Executable File

local timer = require("hs.timer")
local eventtap = require("hs.eventtap")
local events = eventtap.event.types
local module = {}
module.timeFrame = 1
module.action = function()
hs.alert("You double tapped ctrl!")
end
local timeFirstControl = 0
local firstDown = false
local secondDown = false
local noFlags = function(ev)
local result = true
for k,v in pairs(ev:getFlags()) do
if v then
result = false
break
end
end
return result
end
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
local reset = function()
timeFirstControl = 0
firstDown = false
secondDown = false
end
module.eventWatcher = eventtap.new({events.flagsChanged, events.keyDown}, function(ev)
if(timer.secondsSinceEpoch() - timeFirstControl) > module.timeFrame then
reset()
end
if ev:getType() == events.flagsChanged then
if noFlags(ev) and firstDown and secondDown then
if module.action then module.action() end
reset()
elseif onlyCtrl(ev) and not firstDown then
firstDown = true
timeFirstControl = timer.secondsSinceEpoch()
elseif onlyCtrl(ev) and firstDown then
secondDown = true
elseif not noFlags(ev) then
reset()
end
else
reset()
end
return false
end):start()
return module