local function createTap()
if tap then
pcall(function() tap:stop() end)
tap = nil
end
tap = hs.eventtap.new({hs.eventtap.event.types.leftMouseDown}, function(event)
pcall(function()
local pos = hs.mouse.absolutePosition()
local front = hs.application.frontmostApplication()
for _, win in ipairs(hs.window.orderedWindows()) do
local f = win:frame()
if pos.x >= f.x and pos.x <= f.x + f.w and
pos.y >= f.y and pos.y <= f.y + f.h then
local app = win:application()
if app and front and app:pid() ~= front:pid() then
app:activate(true)
end
break
end
end
end)
return false
end)
tap:start()
end
createTap()
-- 每 2 秒检查,僵死就整个重建
hs.timer.doEvery(2, function()
if not tap or not tap:isEnabled() then
createTap()
end
end)