Last active
January 27, 2026 10:36
-
-
Save dainank/fd236aa71a8b3fcf637b9d8428ce98db to your computer and use it in GitHub Desktop.
Hammerspoon Click-Through Support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Helper: Find window under mouse | |
| local function windowUnderMouse() | |
| local mousePos = hs.mouse.absolutePosition() | |
| for _, win in ipairs(hs.window.orderedWindows()) do | |
| if win:isVisible() then | |
| local frame = win:frame() | |
| if mousePos.x >= frame.x and mousePos.x <= frame.x + frame.w and | |
| mousePos.y >= frame.y and mousePos.y <= frame.y + frame.h then | |
| return win | |
| end | |
| end | |
| end | |
| return nil | |
| end | |
| -- Log file setup | |
| local logfilePath = os.getenv("HOME") .. "/hammerspoon_clickthrough.log" | |
| local logfile = assert(io.open(logfilePath, "a")) | |
| local function log(message, level) | |
| level = level or "INFO" | |
| local timestamp = os.date("%Y-%m-%d %H:%M:%S") | |
| logfile:write(string.format("%s [%s] %s\n", timestamp, level, message)) | |
| logfile:flush() | |
| end | |
| -- Eventtap: Focus window under mouse and log click | |
| clickLogger = hs.eventtap.new({hs.eventtap.event.types.leftMouseDown}, function(event) | |
| local win = windowUnderMouse() | |
| local frontmost = hs.window.frontmostWindow() | |
| if win then | |
| if win:id() ~= (frontmost and frontmost:id()) then | |
| win:focus() | |
| log("Focused window: " .. (win:title() or "Untitled")) | |
| else | |
| log("Clicked already-focused window: " .. (win:title() or "Untitled")) | |
| end | |
| else | |
| log("No window under mouse") | |
| end | |
| return false -- allow original click | |
| end) | |
| clickLogger:start() | |
| -- Graceful shutdown | |
| hs.shutdownCallback = function() | |
| log("Hammerspoon shutting down", "INFO") | |
| logfile:close() | |
| end |
adding to autoclave73's code: This will prevent click through Dialogs (e.g. Hammerspoon's Preferences):
clickLogger = hs.eventtap.new({hs.eventtap.event.types.leftMouseDown}, function(event)
local ax = require("hs.axuielement")
local mousePos = hs.mouse.absolutePosition()
local element = ax.systemElementAtPosition(mousePos)
if element then
local role = element:attributeValue("AXRole")
log("role: " .. (role or "Untitled role"))
if role == "AXMenu" or role == "AXMenuItem" then
-- allow original click
log("Skip on role: " .. role)
return false
else
local frontmost = hs.window.frontmostWindow()
-- log("Frontmost window title: " .. (frontmost:title() or "Untitled"))
-- log("Frontmost window subrole: " .. (frontmost:subrole() or "not defined"))
if nil ~= frontmost and frontmost:subrole() == "AXDialog" then
-- allow original click
log("Skip on subrole: " .. frontmost:subrole())
return false
else
local win = windowUnderMouse()
if win then
if win:id() ~= (frontmost and frontmost:id()) then
win:focus()
log("Focused window: " .. (win:title() or "Untitled"))
else
log("Clicked already-focused window: " .. (win:title() or "Untitled"))
end
end
end
end
else
-- allow original click
log("No window under mouse")
end
return false -- allow original click
end)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this will sometimes make you click through and ignore right-click context menus when they display over an unfocused window
This modified Eventtap will detect if there's a context menu under the mouse and allow clicking entries