How to maintain focus on a dialog during text entry?

Get help with using AutoHotkey and its commands and hotkeys
timg11
Posts: 20
Joined: 10 Dec 2016, 14:09

How to maintain focus on a dialog during text entry?

14 Apr 2020, 08:23

I use WinActivate and Keywait to allow the user to enter arbitrary text into a dialog. Keywait passes through the keystrokes into the dialog until triggered by Enter, and then the script resumes and the Enter is sent to terminate the dialog.

Code: Select all

              WinActivate, SomeDialog
              WinSet, Top ,, SomeDialog
              KeyWait, Enter, D T12
              Send {Enter} ;

Of course this assumes SomeDialog has focus, and I'm finding this fails often because other windows steal focus from SomeDialog before or while the text is being entered.

I'd like to have something like

Code: Select all

    
    repeat           
              WinActivate, SomeDialog
              WinSet, Top ,, SomeDialog
              sleep 50
    until KeyWait, Enter, D T12
    Send {Enter} ;
Of course Autohotkey doesn't support the repeat loop, and KeyWait is not a function, but the idea is that it would loop WinActivate until the KeyWait sees its trigger key,

Any ideas how this could be accomplished?
Rohwedder
Posts: 3530
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to maintain focus on a dialog during text entry?

14 Apr 2020, 15:20

Hallo,
try:

Code: Select all

WinActivate, SomeDialog
WinHook(1) ;focus locked
WinSet, Top ,, SomeDialog
KeyWait, Enter, D T12
Send {Enter}
WinHook(0) ;focus unlocked

WinHook(Hook:=False)
{
	Process, Exist
	Script =
	(
		#NoTrayIcon
		HookHWND := WinActive("A")
		SetTimer, TWinHook, 50
		Process, WaitClose, %ErrorLevel%
		ExitApp
		TWinHook:
		WinWaitNotActive, ahk_id `%HookHWND`% ,,1
		IF HookHWND And WinExist("ahk_id " HookHWND)
			WinActivate, ahk_id `%HookHWND`%
		Else
			ExitApp
		Return
	)
	ExecScript(Hook?Script:"")
}
ExecScript(Script:="", Wait:=False)
{ ;https://www.autohotkey.com/docs/commands/Run.htm#ExecScript
	Static
	IF (Script = "")
	{
		exec.Terminate()
		Return
	}
	shell := ComObjCreate("WScript.Shell")
	exec := shell.Exec("AutoHotkey.exe /ErrorStdOut *")
	exec.StdIn.Write(script)
	exec.StdIn.Close()
	if Wait
		return exec.StdOut.ReadAll()
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], draj, Google [Bot], ra8ul and 29 guests