dotfiles/modules/darwin/hammerspoon/Spoons/ControlEscape.spoon/init.lua

121 lines
3.6 KiB
Lua
Raw Normal View History

2021-05-17 15:02:10 +00:00
--- === ControlEscape ===
---
--- Adapted very loosely from https://github.com/jasonrudolph/ControlEscape.spoon
--- Removed timing/delay; always send Escape as well as Control
---
--- Make the `control` key more useful: If the `control` key is tapped, treat it
--- as the `escape` key. If the `control` key is held down and used in
--- combination with another key, then provide the normal `control` key
--- behavior.
local obj={}
obj.__index = obj
-- Metadata
obj.name = 'ControlEscape'
obj.version = '0.1'
obj.author = 'Jason Rudolph <jason@jasonrudolph.com>'
obj.homepage = 'https://github.com/jasonrudolph/ControlEscape.spoon'
obj.license = 'MIT - https://opensource.org/licenses/MIT'
function obj:init()
2021-05-20 00:27:21 +00:00
self.movements = 0
2021-05-17 15:02:10 +00:00
self.sendEscape = false
self.lastModifiers = {}
-- Create an eventtap to run each time the modifier keys change (i.e., each
-- time a key like control, shift, option, or command is pressed or released)
self.controlTap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged},
function(event)
local newModifiers = event:getFlags()
2021-05-19 13:10:37 +00:00
2021-05-20 00:27:21 +00:00
-- If this change to the modifier keys does not involve a *change* to the
2021-05-19 13:10:37 +00:00
-- up/down state of the `control` key (i.e., it was up before and it's
-- still up, or it was down before and it's still down), then don't take
-- any action.
if self.lastModifiers['ctrl'] == newModifiers['ctrl'] then
return false
end
-- Control was not down but is now
2021-05-17 15:02:10 +00:00
if not self.lastModifiers['ctrl'] then
2021-08-30 21:36:04 +00:00
-- Only prepare to send escape if no other modifier keys are in use
2021-05-19 13:10:37 +00:00
self.lastModifiers = newModifiers
if (not self.lastModifiers['cmd'] and not self.lastModifiers['alt']) then
self.sendEscape = true
2021-05-20 00:27:21 +00:00
self.movements = 0
2021-05-19 13:10:37 +00:00
end
2021-08-30 21:36:04 +00:00
2021-05-20 00:27:21 +00:00
-- Control was down and is up, hasn't been blocked by another key, and
-- isn't above the movement threshold
2021-08-30 21:36:04 +00:00
elseif (self.sendEscape == true and not newModifiers['ctrl'] and self.movements < 30) then
2021-05-19 13:10:37 +00:00
self.lastModifiers = newModifiers
2021-08-30 21:36:04 +00:00
2021-05-20 00:27:21 +00:00
-- Allow for shift-escape
2021-05-19 13:10:37 +00:00
if newModifiers['shift'] then
hs.eventtap.keyStroke({'shift'}, 'escape', 0)
else
hs.eventtap.keyStroke(newModifiers, 'escape', 0)
2021-05-17 15:02:10 +00:00
end
2021-05-19 13:10:37 +00:00
self.sendEscape = false
2021-05-20 00:27:21 +00:00
self.movements = 0
2021-08-30 21:36:04 +00:00
self.numberOfCharacters = 0
-- Control was down and is up, but isn't ready to send escape
2021-05-19 13:10:37 +00:00
else
self.lastModifiers = newModifiers
2021-08-30 21:36:04 +00:00
2021-05-17 15:02:10 +00:00
end
2021-05-19 13:10:37 +00:00
end
)
-- If any other key is pressed, don't send escape
self.asModifier = hs.eventtap.new({hs.eventtap.event.types.keyDown},
function(event)
2021-08-30 21:36:04 +00:00
self.sendEscape = false
2021-05-17 15:02:10 +00:00
end
)
2021-05-19 14:35:17 +00:00
2021-05-20 00:27:21 +00:00
-- If mouse is moving significantly, don't send escape
2021-05-19 14:35:17 +00:00
self.scrolling = hs.eventtap.new({hs.eventtap.event.types.gesture},
function(event)
local touches = event:getTouches()
local i, v = next(touches, nil)
while i do
if v["phase"] == "moved" then
2021-05-20 00:27:21 +00:00
-- Increment the movement counter
self.movements = self.movements + 1
2021-05-19 14:35:17 +00:00
end
i, v = next(touches, i) -- get next index
end
end
)
2021-05-17 15:02:10 +00:00
end
--- ControlEscape:start()
--- Method
--- Start sending `escape` when `control` is pressed and released in isolation
function obj:start()
self.controlTap:start()
2021-05-19 13:10:37 +00:00
self.asModifier:start()
2021-05-19 14:35:17 +00:00
self.scrolling:start()
2021-05-17 15:02:10 +00:00
end
--- ControlEscape:stop()
--- Method
--- Stop sending `escape` when `control` is pressed and released in isolation
function obj:stop()
-- Stop monitoring keystrokes
self.controlTap:stop()
2021-05-19 13:10:37 +00:00
self.asModifier:stop()
2021-05-19 14:35:17 +00:00
self.scrolling:stop()
2021-05-17 15:02:10 +00:00
-- Reset state
self.sendEscape = false
self.lastModifiers = {}
end
return obj