Being a recent Mac convert (from Windows), I'm trying to fix some annoyances in MacOS, one of which is managing keyboard layouts. In particular, I want every window to remember its layout. For this I need to detect when the focused window changes, whether this happens within a single application or the active application has changed too.
So, I want my script to remember the keyboard layout _per window_. I guess I should use some combination of hs.application.watcher and hs.uielement.watcher to detect changes in both the active application and active window?
You can subscribe to the window focus event with hs.window.filter.
Is it what you need?
Thanks for the quick reply! For future reference, this piece of code shows an alert whenever the focused window changes:
hs.window.filter.default:subscribe(hs.window.filter.windowFocused, function(window, appName)
alert('Focused: ' .. window:title())
end)
Now what's left to achieve my goal is to remember and restore the keyboard layout for each window when it's focused.
@iliyang hs.settings is probably your best bet for storing the layout values if you want it to be dynamic (i.e. not hard coded in your init.lua).
(I'm going to close this out because I don't think there is any work to be done in Hammerspoon. Please re-open if you disagree!)
I using spaces plugin make it. whereas not perfect .
-- using spaces plugin for move between space.
local spaces = require("hs._asm.undocumented.spaces")
-- move current window to the space sp
function MoveWindowToSpace(sp)
-- local curspaceID = spaces.activeSpace() -- current space id
local win = hs.window.focusedWindow() -- current window
local uuid = win:screen():spacesUUID() -- uuid for current screen
local spaceID = spaces.layout()[uuid][sp] -- internal index for sp
spaces.moveWindowToSpace(win:id(), spaceID) -- move window to new space
spaces.changeToSpace(spaceID) -- follow window to new space
end
hs.hotkey.bind({"control", "command"}, "1", function() MoveWindowToSpace(2) end)
hs.hotkey.bind({"control", "command"}, "2", function() MoveWindowToSpace(3) end)
hs.hotkey.bind({"control", "command"}, "3", function() MoveWindowToSpace(4) end)
Most helpful comment
Thanks for the quick reply! For future reference, this piece of code shows an alert whenever the focused window changes:
Now what's left to achieve my goal is to remember and restore the keyboard layout for each window when it's focused.