Ciantic / VirtualDesktopAccessor

DLL for accessing Windows 11/10 Virtual Desktop features from e.g. AutoHotkey
MIT License
782 stars 93 forks source link

export function to focus window if possible #77

Open theSoberSobber opened 1 year ago

theSoberSobber commented 1 year ago

when using the ahk v2 script it doesn't focus the window in front when it has already switched to a workspace, this is incredibly annoying and I was hoping that there could be a function that could be used to focus already stored window ID or something that had the focus last time. Thanks!

Ciantic commented 1 year ago

Yes, I've noticed this too before. But the purpose of this library is generally not to store data, this is the implementation detail of the AHK script you are making.

You can write it in a script: store the old focused window and restore it on OnChangeDesktop.

theSoberSobber commented 1 year ago

I see. I can def store the last focused in an array or something such that the indices match the desktop number and a[idx] = window id. But how do I focus the window? Like is there a function i call to focus a window with a window ID? How do I get the window ID in the first place? Sorry very new to ahk and dlls in general, don't know much other than js and py that's why confused.

theSoberSobber commented 1 year ago

I'm sorry, I think this works https://www.autohotkey.com/docs/v2/lib/WinActivate.htm I am just not sure on how to make autohotkey preserve that data now, like how do i write to a file, if I do, can the ahk lang allow me to split the input read as a string to an array on a comma delimeter if i wanna store like that etc.? sorry for bothering can you please provide some level of guidance? I am very new to AHK.

Ciantic commented 1 year ago

You don't need to write to a file, you can use an dictionary.

activeWindowByDesktop[current] := activeHwnd

On left side of dictionary you have current desktop number, on right side you have active window handle.

I don't have time to test this myself at the moment. But I had this same setup earlier.

I'll paste some relevant snippets:

WinGet, activeHwnd, ID, A
current := DllCall(GetCurrentDesktopNumberProc, UInt)
isPinned := DllCall(IsPinnedWindowProc, UInt, activeHwnd)
if (isPinned == 0) {
    activeWindowByDesktop[current] := activeHwnd
}

These are from AHK1 days, so you might need to change them.

Idea is to store it before you change desktop and restore it from the dictionary OnChangeDesktop

Ciantic commented 1 year ago

Btw, the reason my example script doesn't do that at the moment is because it should work without these tricks.

But Windows is flaky when changing desktops, sometimes it remembers the focus and sometimes it doesn't.

theSoberSobber commented 1 year ago

hey! thanks for the reply! and sorry for the late reply, got busy somewhere and couldn't even think about this. I did try what you suggested but I am having trouble declaring the activeWindowByDesktop dictionary as when I declare one by doing activeWindowByDesktop={} and then try to declare key value pairs in it, it errors to saying there is no attribute object on the dictionary. Also I don't know how would one go about focusing the window using it's hwnd (WinActive seems to be taking much more than that) and I don't know how to setup the before change desktop hook :((

Could you paste the whole part from your ahk 1 setup? I bet it won't have many changes, Sorry for troubling you again!

theSoberSobber commented 10 months ago

Hey for anyone encountering this issue, I was having a tough time using AHK and navigating the sparse documentation, so I ended up making a simple hotkeydaemon to use this dll here.