Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export function to focus window if possible #77

Open
theSoberSobber opened this issue Oct 12, 2023 · 7 comments
Open

export function to focus window if possible #77

theSoberSobber opened this issue Oct 12, 2023 · 7 comments

Comments

@theSoberSobber
Copy link

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
Copy link
Owner

Ciantic commented Oct 13, 2023

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
Copy link
Author

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
Copy link
Author

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
Copy link
Owner

Ciantic commented Oct 14, 2023

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
Copy link
Owner

Ciantic commented Oct 14, 2023

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
Copy link
Author

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants