Skip to content

Instantly share code, notes, and snippets.

@waj
Created April 25, 2021 02:54
Show Gist options
  • Save waj/27bd2780b85d444ff29a2bacd2fe6c65 to your computer and use it in GitHub Desktop.
Save waj/27bd2780b85d444ff29a2bacd2fe6c65 to your computer and use it in GitHub Desktop.
Hammerspoon directional focus
function directionalFocus(selector)
local current = hs.window.focusedWindow()
local currentFrame = current:frame()
local others = hs.window.orderedWindows()
for i, win in pairs(others) do
local otherFrame = win:frame()
local intersect = otherFrame:intersect(currentFrame)
if intersect.area == 0 and selector(currentFrame, otherFrame, intersect) then
win:focus()
break
end
end
end
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "left", function()
directionalFocus(function(current, other, intersect)
return other.x2 <= current.x and intersect.h ~= 0
end)
end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "right", function()
directionalFocus(function(current, other, intersect)
return other.x >= current.x2 and intersect.h ~= 0
end)
end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "up", function()
directionalFocus(function(current, other, intersect)
return other.y2 <= current.y and intersect.w ~= 0
end)
end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "down", function()
directionalFocus(function(current, other, intersect)
return other.y >= current.y2 and intersect.w ~= 0
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment