Issue with Gui losing focus

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Issue with Gui losing focus

Post by PuzzledGreatly » 17 May 2022, 21:50

Since upgrading to 1.1.34.02 I'm having a problem with a gui not getting focus and as a result my shortcut keys not working. This is happening sporadically but more often than not after I reload the script. When I checked using winspy the script has the focus rather than the Gui meaning I manually have to click the gui to give it focus so my keyboard shortcuts will work. I've tried using a winactivate immediately after the gui is created but that makes no difference. What is a foolproof method to ensure that a specific window always has focus? Thanks.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Issue with Gui losing focus

Post by Rohwedder » 18 May 2022, 01:01

Hallo,
A way to ensure that a particular window always has focus?:
A timer that gives focus to that particular window at sufficiently short intervals.
By the way, what does winspy show when you think the script has the focus?

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 20 May 2022, 22:56

Thanks for the reply and the idea, winspy shows the name of the script. I haven't seen this behaviour in earlier versions.

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 21 May 2022, 05:52

A suggestion is that if you are having difficulty with a script, then post it. Simple. Fast. Gets you a resolution sooner.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 21 May 2022, 07:18

I hesitated to post the script as the whole script is very large. But I downgraded AHK and encountered the same problem so it's nothing to do with the new version. Basically I have a function that creates two Gui's. One appears on the second monitor and one on the main monitor. Here's some sample code:

Code: Select all

Bobs()
{
	static BH, BW, BT, NF, TF, TH
	
	BH := ZH * 0.65
	BW := ZW * 0.45
	BT := BH + BH + BW + ZM + zm
	TH := BH/2
		
	TF := Ceil(ZF * 1.12)
	EF := ZF * 1.5
	
	;This Gui appears on the second monitor if there is one (Z is monitor count) and is a dummy
	Gui, Bibs:New, AlwaysOnTop -caption border Toolwindow hwndBibs, Choose your Action!
	Gui, Bibs:color, %back%
	Gui, Bibs:margin, 0,0
	if Z > 1
	Gui, Bibs:add, picture, backgroundtrans, #choose.png ;screenshot of Bobs
	Gui, Bibs:Show, x%X_% y%Y_%
	Gui, Bibs:maximize
	
	Groupadd, Choice, ahk_id %Bibs%	
	
	;Bobs is the Gui where the actual choice is made
	Gui, Bobs:New, AlwaysOnTop -caption border -dpiscale hwndBobs +ownerBibs, Choose your Action!
	Gui, Bobs:color, bcd7df
	Gui, Bobs:margin, %zm%, %zm%
	Gui, Bobs:font, s%ZF%, bold
	Gui, Bobs:add, text, w%BT% cnavy center gchoice, Choose your Action!
	Gui, Bobs:font, s%TF%
	Gui, Bobs:add, picture,x%zm% w%BH% h-1 gChoice, #doom1.png
	Gui, Bobs:add, progress, x+%zm% w%BW% h%BH% c165c9e Background165c9e disabled -E0x0200
	Gui, Bobs:add, text, xp0 yp0 w%BW% h%BH% cwhite left BackgroundTrans -border 0x0201 gChoice, % " Save the food!"
	Gui, Bobs:add, picture,x+%zm% w%BH% h-1 gChoice, #save1.png
	Gui, Bobs:add, picture,x%zm% w%BH% h-1 gChoice, #doom2.png
	Gui, Bobs:add, progress, x+%zm% w%BW% h%BH% c165c9e Background165c9e disabled -E0x0200
	Gui, Bobs:add, text, xp0 yp0 w%BW% h%BH% cwhite left BackgroundTrans -border 0x0201 gChoice, % " Save the scamp!"
	Gui, Bobs:add, picture,x+%zm% w%BH% h-1 gChoice, #save2.png
	Gui, Bobs:add, picture,x%zm% w%BH% h-1 gChoice left, #doom3.png
	Gui, Bobs:add, progress, x+%zm% w%BW% h%BH% c165c9e Background165c9e disabled -E0x0200
	Gui, Bobs:add, text, xp0 yp0 w%BW% h%BH% cwhite left BackgroundTrans -border 0x0201 gChoice, % " Save the shark!"
	Gui, Bobs:add, picture,x+%zm% w%BH% h-1 gChoice, #save3.png
	Gui, Bobs:add, picture,x%zm% w%BH% h-1 gChoice left, #doom4.png
	Gui, Bobs:add, progress, x+%zm% w%BW% h%BH% c165c9e Background165c9e disabled -E0x0200
	Gui, Bobs:add, text, xp0 yp0 w%BW% h%BH% cwhite left BackgroundTrans -border 0x0201 gChoice, % " Save the trees!"
	Gui, Bobs:add, picture,x+%zm% w%BH% h-1 gChoice, #save4.png	
	Gui, Bobs:add, picture,x%zm% w%BH% h-1 gChoice left, #save5.png
	Gui, Bobs:add, progress, x+%zm% w%BW% h%BH% c165c9e Background165c9e disabled -E0x0200
	Gui, Bobs:add, text, xp0 yp0 w%BW% h%BH% cwhite left BackgroundTrans -border 0x0201 gChoice, % " Stop  the  war!"
	Gui, Bobs:add, picture,x+%zm% w%BH% h-1 gChoice, #doom5.png	
	Gui, Bobs:Show, autosize center	
	
	Groupadd, Choice, ahk_id %Bobs%	
	
	Winactivate, ahk_group Choice
	
	wingettitle, test,a
	if !instr(test,"Choose your Action!")
	msgbox, 4096, ttest,% test
	
	return
	
	BibsGuiEscape:
	BobsGuiEscape:
	reload
	return
	
	Choice:
	
	msgbox, 4096, Choice,% A_guicontrol
	
	Return
}
Having run this lots of times when the gui doesn't get focus usually the test msgbox fails to fire as well (but not always). I can't see anything in the above that would stop Bobs from having focus.

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 21 May 2022, 07:23

Your script generates an error due to undefined variables. If you post a script-- good idea as I mentioned-- I would also recommend that the reader be able to test it by running it. You can verify its viability by running the script yourself before posting it.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 21 May 2022, 07:45

Thanks Mikey, I get your point but if I rewrite the script to include all the variables then it is no longer simple or fast to post the script. They are primarily dimensions. The gui also has pngs as buttons so they won't show either. I guess a cut down script would be as follows:

Code: Select all


Bobs()
return

Bobs()
{
	static BH, BW, BT, NF, TF, TH
	
	;This Gui appears on the second monitor if there is one (Z is monitor count) and is a dummy
	Gui, Bibs:New, AlwaysOnTop -caption border Toolwindow hwndBibs, Choose your Action!
	Gui, Bibs:color, %back%
	Gui, Bibs:margin, 20,20
	Gui, Bibs:add, text,, Choose your Action!
	Gui, Bibs:Show, x1920 y0
	Gui, Bibs:maximize
	
	Groupadd, Choice, ahk_id %Bibs%	
	
	;Bobs is the Gui where the actual choice is made
	Gui, Bobs:New, AlwaysOnTop -caption border -dpiscale hwndBobs +ownerBibs, Choose your Action!
	Gui, Bobs:color, bcd7df
	Gui, Bobs:margin, 20, 20
	Gui, Bobs:font, s30, bold
	Gui, Bobs:add, text, cnavy center gchoice, Choose your Action!
	Gui, Bobs:Show, autosize center	
	
	Groupadd, Choice, ahk_id %Bobs%	
	
	Winactivate, ahk_group Choice
	
	wingettitle, test,a
	if !instr(test,"Choose your Action!")
	msgbox, 4096, ttest,% test
	
	return
	
	BibsGuiEscape:
	BobsGuiEscape:
	exitapp
	return
	
	Choice:
	
	msgbox, 4096, Choice,% A_guicontrol
	
	Return
}

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 21 May 2022, 08:19

Your question refers to shortcut keys that do not work, but your script contains none. I'm not sure what you expect the reader to do to assess your situation. Write our own script, and then test it?

My impression of WinActivate, ahk_group Choice is that it will activate only the topmost matching window.

Explained: GroupAdd
All windowing commands can operate upon a window group by specifying ahk_group MyGroupName for the WinTitle parameter. The commands WinMinimize, WinMaximize, WinRestore, WinHide, WinShow, WinClose, and WinKill will act upon all the group's windows.

Code: Select all

Loop {
 WinWaitNotActive, ahk_class AutoHotkeyGUI
 WinActivate
 SoundBeep, 1500
}

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 21 May 2022, 18:52

My apologies, mikey. I was very tired when I replied last night and cut corners. The following is similar to my script except that it lacks images. Following your comment about groups I've changed the Winactivate line. What I can't work out is why the focus wouldn't be on one of the newly created Guis.

Code: Select all


Bobs()
return

Bobs()
{
	;This Gui appears on the second monitor and is a dummy for display only
	Gui, Bibs:New, AlwaysOnTop -caption border Toolwindow hwndBibs, Choose your Action!
	Gui, Bibs:color, %back%
	Gui, Bibs:margin, 20,20
	Gui, Bibs:add, text,, Choice One
	Gui, Bibs:add, text,, Choice Two
	Gui, Bibs:Show, x1920 y0
	Gui, Bibs:maximize
	
	Groupadd, Choice, ahk_id %Bibs%	
	
	;Bobs is the Gui where the actual choice is made
	Gui, Bobs:New, AlwaysOnTop -caption border -dpiscale hwndBobs +ownerBibs, Choose your Action!
	Gui, Bobs:color, bcd7df
	Gui, Bobs:margin, 20, 20
	Gui, Bobs:font, s30, bold
	Gui, Bobs:add, text, cnavy center gchoice, Choice One
	Gui, Bobs:add, text, cnavy center gchoice, Choice Two
	Gui, Bobs:Show, autosize center	
	
	Groupadd, Choice, ahk_id %Bobs%	
	
	Winactivate, ahk_id %Bobs%
	
	wingettitle, test,a
	if !instr(test,"Choose your Action!")
	msgbox, 4096, test,% test
	
	return
	
	BibsGuiEscape:
	BobsGuiEscape:
	exitapp
	return
	
}

Choice(i:= "")
{
	info := i = 1 ? "Choice One" : i = 2 ? "Choice Two" : A_GuiControl
	msgbox, 4096, Choice,% info
}

#ifwinactive, ahk_group Choice

	1:: Choice(A_thishotkey)
	2:: Choice(A_thishotkey)
	
#ifwinactive 

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 21 May 2022, 19:07

In my testing with a single monitor, the script posted here yielded an active GUI.
The following is similar to my script...
A different script may yield different results.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 22 May 2022, 06:14

Yes, I still have the problem with loss of focus around 20% of the time.

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 22 May 2022, 06:28

With the posted script, or your own different script?

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 22 May 2022, 06:53

Thanks for the reply. With my whole script. When Bobs() is called I don't have any other gui windows running. I tried using Critical and Threads, NoTImers but they made no difference. Does anyone have any thoughts on what could be moving the focus from the gui to the script itself?

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 22 May 2022, 07:34

We should identify this without seeing your script?

A suggestion: if you can't post your script, then post a script that demonstrates the problem that you are trying to solve.

I'm having trouble debugging a script that is not posted. I will hand it to the experts here!

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 22 May 2022, 17:00

Thanks, mikey. I think if I knew how to post a script demonstrating this problem then I would also know the solution. In my experience when a script creates and shows a gui that gui window has focus. In this case the gui is losing focus some of the time. I'm after ideas for things I can check as to what could cause sporadic loss of focus.

Edit: to try to find out the cause I removed Bibs from my script and changed the first line of Bobs as follows:

Code: Select all

Gui, Bobs:New, AlwaysOnTop -caption border -dpiscale hwndBobs, Choose your Action!
I've now noticed that when the issue occurs I have two tabs on the task bar, one with the name of the script and Bobs. How could the script have a taskbar button by itself?
Last edited by PuzzledGreatly on 22 May 2022, 17:17, edited 1 time in total.

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 22 May 2022, 17:05

Sure, OK, but....

1. Activating a different window
2. Deactivating your window

I'm not being silly or difficult here-- these are really the two things! The issue is that one would need to search through your specific code to find them. So: WinActivate, Run, Gui, ControlSend, ControlFocus, ControlClick, Click, WinSet, and so on-- lots of possibilities. Processes other than your own script can also steal focus. Perhaps you can now understand the reasoning behind my earlier comments.

I would also argue that identifying a problem does not necessarily entail identifying a solution as you suggest-- though agree that replicating the problem may be difficult if you are not sure where to look.

There is always the brute-force, blind method: delete parts of your script until the problem disappears! You will have then solved the puzzle. Actually, this is a very fast technique. Start by deleting half of the script. If the problem disappeared, restore half of what was deleted. If the problem remains, delete half of what remains. Repeat. That will likely pinpoint the issue within about ten minutes.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 22 May 2022, 18:41

Thanks for the reply which I saw after posting my edit. I agree in general that identifying the problem does not bring about the solution but in this case I think it does. The point is that this is happening sporadically. So what ever is causing the problem is not happening all the time. I think the problem lies in the script because it has occurred when I've been running nothing else. I've tried eliminating parts of the script and but the problem remains. I've been looking at this for hours now. How can the script have a separate tab on the tool bar? There is no other gui. Thanks for the list of suggestions to look at.

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 22 May 2022, 18:56

Below is just one example of how one can goof with Gui coding.

Code: Select all

Gui, Bobs:New
Gui, Add, Text, w250, Test1
SetTimer, F3, -10
F3::Gui, Show,, Gui1
There are so many ways!

Assigned homework: https://www.autohotkey.com/docs/commands/Gui.htm#New

I'll sign off here, wishing you luck.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Issue with Gui losing focus

Post by PuzzledGreatly » 22 May 2022, 21:34

Thanks mikey, I've read the help info about Guis many times but your example gave me the inspiration to solve the problem. I thought it must be something to do with a timer and I had overlooked one that ran once:

Code: Select all

Gui, Emb:submit, nohide
SetTimer, ShowDialogue, -2000
return

ShowDialogue:
Gui, Emb:show
Return
So If the user cleared Emb quickly the timer tried to show a gui that no longer existed and hence stole the focus from the one that did. The solution is trivial - just switch the timer off when Emb is submitted. As I suspected finding the problem immediately offered the solution. One takeaway from this is that when problems are happening sporadically first check all timers! Thanks again for the example that led me to the answer. Even if I could have posted the whole code I wanted to figure the issue out on my own so as to avoid it in the future. The problem manifesting just after I upgraded AHK was an unfortunate coincidence and a red herring.

User avatar
mikeyww
Posts: 26592
Joined: 09 Sep 2014, 18:38

Re: Issue with Gui losing focus

Post by mikeyww » 23 May 2022, 03:28

Good to hear. For anyone who is interested, the problem is perhaps demonstrated in a one-line script.

Code: Select all

Gui, Show
One could consider this an AHK bug depending on the view: should it be possible to show a Gui if the Gui has no "content" (controls), and the Gui is autosized to have a width and height of zero? I do not have an answer, but others can ponder it (and probably already do have an answer). I guess there could be a reason somewhere to show a Gui as an empty window on the screen, because one can change colors, remove the caption, and essentially just show a rectangle.

Code: Select all

Gui, -Caption
Gui, Color, Red
Gui, Show, w100 h100

Post Reply

Return to “Ask for Help (v1)”