Winactivate sets focus on first edit field

Report problems with documented functionality
User avatar
Posts: 3
Joined: 27 Aug 2020, 09:47

Winactivate sets focus on first edit field

Post by ManJelL » 09 Apr 2021, 07:33

Not sure if this is actually a bug but when a gui is minimized and winactive is used on it, it sets focus on the first edit field of that window. It only does that when the window is minimized.

I'm using Windows 10 and Autohotkey version

Here is an example:

Code: Select all

Gui,testgui: new
gui, add, edit, w50 h20 x20 y20
gui, add, edit, w50 h20 x+20
gui, add,button, x+50, testbutton
gui,show, x900 y200 w500 h500, testgui
guicontrol ,focus, testbutton


winactivate, testgui


Posts: 7412
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Winactivate sets focus on first edit field

Post by lexikos » 09 Apr 2021, 16:38

WinActivate doesn't set focus. The window or window manager sets focus when it gains activation.

The behaviour of saving/restoring focus when a window loses/gains focus is neither guaranteed nor implemented by AutoHotkey. It is implemented by the system function DefDlgProc, along with most other standard dialog behaviour.

The focus issue seems to be a consequence of restoring the window prior to activating it, although even that behaves as expected if the window was minimized by clicking its taskbar button. I was unable to determine exactly which combination of messages is causing the undesired switch of focus, but I could see that the Button has focus when focus is switched to the Edit.

This has the same effect:

Code: Select all

DllCall("ShowWindow", "ptr", WinExist("testgui"), "int", 9) ; SW_RESTORE
This appears to have the desired effect:

Code: Select all

hwnd := WinExist("testgui")
DllCall("SetForegroundWindow", "ptr", hwnd)
DllCall("ShowWindow", "ptr", hwnd, "int", 9) ; SW_RESTORE

Changing WinActivate to restore the window after activation would avoid this issue, but I hesitate to do it because it may have other consequences; also, the documented behaviour is (emphasis added):
If the window is minimized and inactive, it is automatically restored prior to being activated.

Post Reply

Return to “Bug Reports”