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

tim*_*irg 6 karabiner hammerspoon macos-sierra

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

由于 karabiner 消失了,我一直在尝试使用hammerspoon 恢复我最喜欢的功能,我认为这可以实现,但我无法使其正常工作,有人知道如何正确执行此操作吗?

jos*_*ird 6

-- 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()
Run Code Online (Sandbox Code Playgroud)