isolate escape from control key

This commit is contained in:
Noah Masur 2021-05-19 09:10:37 -04:00
parent 269e7a1225
commit 4348c52b1f

View File

@ -27,19 +27,41 @@ function obj:init()
self.controlTap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, self.controlTap = hs.eventtap.new({hs.eventtap.event.types.flagsChanged},
function(event) function(event)
local newModifiers = event:getFlags() local newModifiers = event:getFlags()
-- If this change to the modifier keys does not invole a *change* to the
-- 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
if not self.lastModifiers['ctrl'] then if not self.lastModifiers['ctrl'] then
if newModifiers['ctrl'] then self.lastModifiers = newModifiers
if (newModifiers['shift'] and newModifiers['cmd']) then if (not self.lastModifiers['cmd'] and not self.lastModifiers['alt']) then
; self.sendEscape = true
elseif newModifiers['shift'] then end
-- Control was down and is up
elseif (self.sendEscape == true and not newModifiers['ctrl']) then
self.lastModifiers = newModifiers
if newModifiers['shift'] then
hs.eventtap.keyStroke({'shift'}, 'escape', 0) hs.eventtap.keyStroke({'shift'}, 'escape', 0)
else else
hs.eventtap.keyStroke(newModifiers, 'escape', 0) hs.eventtap.keyStroke(newModifiers, 'escape', 0)
end end
end self.sendEscape = false
end else
self.lastModifiers = newModifiers self.lastModifiers = newModifiers
end end
end
)
-- If any other key is pressed, don't send escape
self.asModifier = hs.eventtap.new({hs.eventtap.event.types.keyDown},
function(event)
self.sendEscape = false
end
) )
end end
@ -48,6 +70,7 @@ end
--- Start sending `escape` when `control` is pressed and released in isolation --- Start sending `escape` when `control` is pressed and released in isolation
function obj:start() function obj:start()
self.controlTap:start() self.controlTap:start()
self.asModifier:start()
end end
--- ControlEscape:stop() --- ControlEscape:stop()
@ -56,6 +79,7 @@ end
function obj:stop() function obj:stop()
-- Stop monitoring keystrokes -- Stop monitoring keystrokes
self.controlTap:stop() self.controlTap:stop()
self.asModifier:stop()
-- Reset state -- Reset state
self.sendEscape = false self.sendEscape = false