Focus and FocusedCtrl within MyGui lose focus

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Frigol
Posts: 5
Joined: 22 Sep 2022, 06:57

Focus and FocusedCtrl within MyGui lose focus

Post by Frigol » 30 Sep 2022, 02:34

I've problems in using the focus commands in the correct manner within the AHK Gui itself. I wish to copy automatically some text from the browser to a text field through the clipboard. Than the script should jump (focus) to the next textfield and wait for new clipboard content. In addition I wish to be able to skip a text field through clicking on the next field with the mouse. But somehow I don't get the focus or lose it when clicking in an other text field and switching back to the browser. Do I have to activate the Gui windows itself before the focus command?

Code: Select all

MyGui := Gui()
	MyGui.Add("Text",, "Position:")
	MyGui.Add("Text",, "Company:")
	MyGui.Add("Text",, "Contact (optional):")
	PositionClipChange := MyGui.Add("Edit", "vPositionName ys w256") ; Stelle
	NameClipChange := MyGui.Add("Edit", "vCompanyName w256")  ; Start a new column within this section. ; Firmenname
	ContactClipChange := MyGui.Add("Edit", "vContactName w256") ; Ansprechpartner
	OKClipChange := MyGui.Add("Button", "default", "OK").OnEvent("Click", ProcessUserInput)
	MyGui.OnEvent("Close", ProcessUserInput)
	MyGui.Show

	OnClipboardChange ClipChanged

	ClipChanged(clip_type)
	{
		;if WinActive("ahk_exe vivaldi.exe")
		;{
		if MyGui.FocusedCtrl = PositionClipChange
			{
			MyGui["PositionName"].Text := A_Clipboard
			MyGui["CompanyName"].Focus()
			}
		else if MyGui.FocusedCtrl = NameClipChange
			{
			MyGui["CompanyName"].Text := A_Clipboard
			MyGui["ContactName"].Focus()
			}
		else if MyGui.FocusedCtrl = ContactClipChange
			{
			MyGui["ContactName"].Text := A_Clipboard
			MyGui["OK"].Focus()
			}
		else if MyGui.FocusedCtrl = OKClipChange
			{
			}
		;}
		; A_Clipboard := ""
	}

	ProcessUserInput(*)
	{
	}

Frigol
Posts: 5
Joined: 22 Sep 2022, 06:57

Re: Focus and FocusedCtrl within MyGui lose focus

Post by Frigol » 30 Sep 2022, 10:40

I found a fix. Placing "MyGui.Show" in the first line of "Clipchanged(clip_type)" makes the foucs commands work. But I think they should work without this fix as it is described here:

"Focused Retrieves the current focus state of the control. RetrievedState := GuiCtrl.Focused Note: To be effective, the window generally must not be minimized or hidden.

lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Focus and FocusedCtrl within MyGui lose focus

Post by lexikos » 30 Sep 2022, 17:55

What you quoted doesn't appear to support your statement. Also, you aren't using the Focused property.

When you deactivate the GUI, the control loses focus. FocusedCtrl therefore correctly tells you that no control is focused. When you reactivate the window, the previously focused control is reactivated (by the dialog manager; the program itself doesn't know which control was focused).

Also, OKClipChange contains an empty string (the return value of OnEvent).

Post Reply

Return to “Ask for Help (v2)”