hammerspoon 重映射控制键:单独按下时发送 esc,与其他键一起按下时发送控制

hammerspoon remap control key: sends esc when pressed alone, send control when pressed with other keys

在我看来,这是一个非常有用的重映射,因为您几乎从不单独键入 control,为什么不将它重映射到 esc?

自从 karabiner 消失后,我一直在尝试使用 hammerspoon 恢复我最喜欢的功能,我认为这可以实现,但我就是无法让它工作,有人知道如何正确地做到这一点吗?

-- Sends "escape" if "caps lock" is held for less than .2 seconds, and no other keys are pressed.

local send_escape = false
local last_mods = {}
local control_key_timer = hs.timer.delayed.new(0.2, function()
    send_escape = false
end)

hs.eventtap.new({hs.eventtap.event.types.flagsChanged}, function(evt)
    local new_mods = evt:getFlags()
    if last_mods["ctrl"] == new_mods["ctrl"] then
        return false
    end
    if not last_mods["ctrl"] then
        last_mods = new_mods
        send_escape = true
        control_key_timer:start()
    else
        if send_escape then
            hs.eventtap.keyStroke({}, "escape")
        end
        last_mods = new_mods
        control_key_timer:stop()
    end
    return false
end):start()


hs.eventtap.new({hs.eventtap.event.types.keyDown}, function(evt)
    send_escape = false
    return false
end):start()