I have struggled to set an InputBox in AutoHotkey as modal/ always on top. Unfortunately, there is no built-in option for this. I share here my learning and final solution.
References/ Background
It is quite a common request to want an InputBox to force the user to enter or close it.
I had this bad user experience in my Teamsy Launcher, where I couldn't find the inputbox window and forgot it was already opened, having then the script hanging until you notice it and close it.
First solution: Gui +LastFound +OwnDialogs +AlwaysOnTop
The first solution I have found was in Jack's blog post Use Gui Owndialogs To Glue Modal Dialogs Boxes To Gui Parent Windows Autohotkey Best Practice
Before calling InputBox, place this line of code:
Gui +LastFound +OwnDialogs +AlwaysOnTop ; set inputbox modal
It worked fine but I have noticed following drawback:
After you close the InputBox the focus on the last active window is lost.
So I couldn't use this to run a function that required after the inputbox to get the selection of the last active window.
Second solution: Own Gui
To workaround this drawback, I have come to finally use my own GUI for a modal InputBox.
See example below:
TeamsyInputBox(){
static
ButtonOK:=ButtonCancel:= false
Gui GuiTeamsy:New,, Teamsy
;Gui, add, Text, ;w600, % Text
Gui, add, Edit, w190 vTeamsyEdit
Gui, add, Button, w60 gTeamsyOK Default, &OK
Gui, add, Button, w60 x+10 gTeamsyHelp, &Help
Gui +AlwaysOnTop -MinimizeBox ; no minimize button, always on top-> modal window
Gui, Show
while !(ButtonOK||ButtonCancel)
continue
if ButtonCancel {
ErrorLevel := 1
return
}
Gui, Submit
ErrorLevel := 0
return TeamsyEdit
;----------------------
TeamsyOK:
ButtonOK:= true
return
;----------------------
TeamsyHelp:
Run, "https://tdalon.github.io/ahk/Teamsy"
GuiTeamsyGuiEscape:
GuiTeamsyGuiClose:
ButtonCancel:= true
Gui, Cancel
return
}
Note: the use of -MinimizeBox to hide a minimize button and Gui +AlwaysOnTop to make the Gui "modal".
I prefer finally this solution, also because the OK button can be set as default so when you enter something in the box and hit OK it will close the window.
Alternatively one could use a timer as explained in this AutoHotkey forum post.
No comments:
Post a Comment