← Back to context

Comment by overflowy

10 hours ago

I use this to remap app keys:

    local appHotkeys = {}

    local function remapAppHotkey(appName, fromMods, fromKey, toMods, toKey, delay)
        if not appHotkeys[appName] then
            appHotkeys[appName] = {}
        end
        local hotkey = hs.hotkey.new(fromMods, fromKey, function()
            hs.eventtap.keyStroke(toMods, toKey, delay or 0)
        end)
        table.insert(appHotkeys[appName], hotkey)
    end
    
    local appWatcher = hs.application.watcher.new(function(appName, eventType)
        local hotkeys = appHotkeys[appName]
        if not hotkeys then return end
        for _, hotkey in ipairs(hotkeys) do
            if eventType == hs.application.watcher.activated then
                hotkey:enable()
            elseif eventType == hs.application.watcher.deactivated then
                hotkey:disable()
            end
        end
    end)
    
    appWatcher:start()

    -- Remap app hotkeys
    remapAppHotkey("Finder", { "cmd" }, "q", { "cmd" }, "w", 0.5)
    ... etc ...