Skip to content

Instantly share code, notes, and snippets.

@spadino
Created March 6, 2016 16:06
Show Gist options
  • Save spadino/a62be95f7c3bbf53709d to your computer and use it in GitHub Desktop.
Save spadino/a62be95f7c3bbf53709d to your computer and use it in GitHub Desktop.
Move window to space at left or right just with keyboard, using hammer spoon. Complementing setup for KWM users.
-- Switch windows to next space at left or right
function moveWindowOneSpace(direction)
_mouseOrigin = hs.mouse.getAbsolutePosition()
local win = hs.window.focusedWindow()
_clickPoint = win:zoomButtonRect()
_clickPoint.x = _clickPoint.x + _clickPoint.w + 5
_clickPoint.y = _clickPoint.y + (_clickPoint.h / 2)
local mouseClickEvent = hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftmousedown, _clickPoint)
mouseClickEvent:post()
hs.timer.usleep(150000)
local nextSpaceDownEvent = hs.eventtap.event.newKeyEvent({"ctrl"}, direction, true)
nextSpaceDownEvent:post()
end
function moveWindowOneSpaceEnd(direction)
local nextSpaceUpEvent = hs.eventtap.event.newKeyEvent({"ctrl"}, direction, false)
nextSpaceUpEvent:post()
hs.timer.usleep(150000)
local mouseReleaseEvent = hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.leftmouseup, _clickPoint)
mouseReleaseEvent:post()
hs.timer.usleep(100000)
hs.mouse.setAbsolutePosition(_mouseOrigin)
end
hk1 = hs.hotkey.bind({"ctrl","cmd"}, "right",
function() moveWindowOneSpace("right") end,
function() moveWindowOneSpaceEnd("right") end)
hk2 = hs.hotkey.bind({"ctrl","cmd"}, "left",
function() moveWindowOneSpace("left") end,
function() moveWindowOneSpaceEnd("left") end)
@spadino
Copy link
Author

spadino commented Mar 6, 2016

Working with Hammerspoon 0.9.45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment