OSD GUI function -- problems when sending keys

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

OSD GUI function -- problems when sending keys

Post by elbitjusticiero » 07 Dec 2022, 05:53

Hi all, I have a problem with a function I wrote. It's an OSD function that creates a GUI and keeps adding text whenever called. It's for telling the user about the steps of a process, for example, you could use it to display "Writing file 1", then "Writing file 2", and so on.

What I do in this function is save the current window before updating the GUI, then reactivating the previous window in order to keep working with it. The problem is... it doesn't work. Whenever I'm using this function, trying to send or paste text to the primary window fails. The speaker beeps, because, I think, AHK is trying to send text to the OSD GUI, which shouldn't be happening.

I'll paste the whole function here. If somebody could point me to the source of the problem, I'd be very grateful.

Thanks!

Code: Select all

OSD( cmd := "--", params := "" ) {
	Static x := 33
	Static y := 33
	Static text := ""
	Static fgcolor := "white"
	Static bgcolor := "blue" 
	Static font := "Inconsolata"
	Static fsize := 12
	
	hw := WinActive("A")		; here is where I save the current window
	
	Switch cmd
	{
		Case "INIT": 
			Gui, OSD:New, +AlwaysOnTop -Caption +ToolWindow
			Gui, OSD:Color, blue
			Gui, OSD:Font, cWhite s12, Inconsolata
			Gui, OSD:Margin, 6
			
		Case "--": 
			Sleep % params
			Gui, OSD:Destroy
		Default: 
			text .= cmd "`n"
			if IsObject(params) {
				for e, v in params {
					%e% := v
					Gosub OSD_SetVars 
				}
			}
			Gui, OSD:+LastFoundExist 
			if not WinExist() 
				OSD("INIT") 
			Gui, OSD:Add, Text, Y+4, % cmd
			Gui, OSD:Show, X%x% Y%y% Autosize 
	}
	WinActivate, %hw%		; restoring the active window
	Sleep 333				; I put this in just in case
	
	return
	
	OSD_SetVars:
		Gui, OSD:Color, %bgcolor%
		Gui, OSD:Font, c%fgcolor% s%fsize%, %font%
	return
}

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

Re: OSD GUI function -- problems when sending keys

Post by mikeyww » 07 Dec 2022, 06:03

WinActivate accepts a :arrow: WinTitle, not a HWND by itself.
Use ahk_id HWND in WinTitle to identify a window or control by its unique ID.
Gui, Show also has a NoActivate option.

elbitjusticiero
Posts: 75
Joined: 06 May 2017, 11:07

Re: OSD GUI function -- problems when sending keys

Post by elbitjusticiero » 07 Dec 2022, 09:02

mikeyww wrote:
07 Dec 2022, 06:03
WinActivate accepts a :arrow: WinTitle, not a HWND by itself.
Use ahk_id HWND in WinTitle to identify a window or control by its unique ID.
Gui, Show also has a NoActivate option.
Well, not only you solved the issue, but I learned TWO things from your comment.

Thank you so much!

Post Reply

Return to “Ask for Help (v1)”