Move focus to another display with keyboard
I'm using Yosemite. I have two external displays with several spaces (I like the idea one app per space). Sometimes I want to move focus to another display without using a mouse. I have few solutions but they are not ideal.
cmd+F4 Move focus to next or another window in System Preferences - it moves focus but mouse still stays on previous display so moving spaces does not work properly (ctrl+←or→ moves spaces on previous display)
BetterTouchTool can move mouse, but mouse position should be hardcoded. What is more moving a mouse does not cause moving the focus, you should click additionally and it's not always good
ctrl+n can move to n space, but it does not work if space is opened on another screen e.g. if space 1 is focused on display 1 and space 2 is opened on display 2 then pressing ctrl+2 does not move focus, but pressing ctrl+3 and then ctrl+2 does (if space 3 is created on display 2)
Do you have any ideas?
Summary:
- Install hammerspoon
- Copy the following code into init.lua and run hammerspoon
- Press Cmd+1 to toggle between screens
For people who are looking for another solution to this, consider using hammerspoon (which is a great application that lets you write scripts for automating osx using lua). I've included the following code in my init.lua file to implement screen switching upon pressing Cmd+1.
local function focus_other_screen() -- focuses the other screen
local screen = hs.mouse.getCurrentScreen()
local nextScreen = screen:next()
local rect = nextScreen:fullFrame()
local center = hs.geometry.rectMidPoint(rect)
hs.mouse.setAbsolutePosition(center)
end
function get_window_under_mouse() -- from https://gist.github.com/kizzx2/e542fa74b80b7563045a
local my_pos = hs.geometry.new(hs.mouse.getAbsolutePosition())
local my_screen = hs.mouse.getCurrentScreen()
return hs.fnutils.find(hs.window.orderedWindows(), function(w)
return my_screen == w:screen() and my_pos:inside(w:frame())
end)
end
function activate_other_screen()
focus_other_screen()
local win = get_window_under_mouse()
-- now activate that window
win:focus()
end
hs.hotkey.bind({"cmd"}, "1", function() -- does the keybinding
activate_other_screen()
end)
This works by moving the mouse to the other screen, then focusing the application which is directly under the mouse. The code is in lua. Please see hammerspoon for more details.
Nevermind, I found an app which does it - Amethyst. I've been looking for something like that for ages