Gathering textual user input is a common task, and using AutoHotkey's InputBox command is a great simple way to do this. The following function corrects many shortcomings of the InputBox command, adds a few features, and makes it easier to use. You should have this script in your library if you use InputBoxes.
InputBox(ByRef OutputVar, Title="", Text="", Default="`n", Keystrokes="")
Usage: The most common way to use this function is as follows:
#g:: If InputBox(Var) Return MsgBox, You entered the text "%var%" and submitted the InputBox. Now the script can do something with this input. Return
Usually, an InputBox allows the user the opportunity to either enter text OR cancel the command then and there if they wish. Since the function returns the ErrorLevel of the InputBox command, this is an easy way to return if the user cancels the box.
https://ahknet.autoh...izontalLine.png
Parameters:[*:f548ouio]OutputVar: This is the variable in which the result will be stored. Leave it unquoted since the parameter is ByRef. Basically it's analogous to the first parameter in the regular InputBox command except that in this case the variable is not changed if the user cancels the box.[*:f548ouio]Title: The title of the dialog box. The default title is "NameOfScript: Input"[*:f548ouio]Text: The text prompt to display. Normally the box is only tall enough for the titlebar, textbox, and buttons, but if you put text here the function will try to automatically make room for it. It judges by the number of newlines (`n) present, so just add more at the end of the "text" param if the box doesn't end up being tall enough for all the text.[*:f548ouio]Default: The text already present in the input field.
The default is "`n", which is a placeholder that means that the input will contain whichever text was last submitted to that particular input box, blank if none. The function distinguishes between prior input boxes based on title. So it pays to give your InputBoxes unique titles, because that way a different last input is remembered for each.
Sometimes, it is desirable to change the title of a box while maintaining the same functionality. Any text after " - " does not affect the input box memory, for instance, "Name - John Doe" and "Name - Jane Smith" will be considered the same.[*:f548ouio]Keystrokes: Keystrokes to send to the input field before the user interacts with it. Normally, the entirety of the default text is selected. The keystrokes parameter can be used to change this, for instance, to select a file name and exclude its extension as happens when you press F2 in Windows 7.
https://ahknet.autoh...izontalLine.png
Why use InputBox()?[*:f548ouio]Shortens code required to make an InputBox. Also, functions are generally more versatile than commands.
[*:f548ouio]InputBoxes are always-on-top.
[*:f548ouio]Simplifies the long and confusing list of parameters that come with the built-in command by assuming some (x, y, width, height) and removing others (font, HIDE, timeout).https://ahknet.autohotkey.com/~berban/spacer.pnghttps://ahknet.autohotkey.com/~berban/spacer.pnghttps://ahknet.autohotkey.com/~berban/HorizontalLine.png
Some working examples:
While !InputBox(VarA, "VarA") and !InputBox(VarB, "VarB") ;continues until one returns an error MsgBox, You entered "%VarA%" and "%VarB%" MsgBox, You cancelled one of the above InputBoxes. ;Dialog box to create a new file on your desktop !^n:: NewFile = NewFile.txt Loop If InputBox(NewFile, "New File...", A_Index = 1 ? "" : """" NewFile """ already exists.", NewFile, "{Home}+{Right " InStr(NewFile, ".", 0, 0) - 1 "}") Return Else If !FileExist(A_Desktop "\" NewFile) Break FileAppend, , %A_Desktop%\%NewFile% Run, notepad.exe "%A_Desktop%\%NewFile%" Return ;One line google search! ;Hold CTRL while submitting to search AutoHotkey.com ;Hold SHIFT while submitting to do an I'm Feeling Lucky™ search. ;Try doing both together F4::Run, % InputBox(SearchTerms, "Google™ Search") ? "" : "http://www.google.com/search?q=" RegExReplace(SearchTerms, "\s+", "+") (GetKeyState("Ctrl", "P") ? "+site:AutoHotkey.com" : "") (GetKeyState("Shift", "P") ? "&btnI=3564" : "")
https://ahknet.autoh...izontalLine.png
Code:
InputBox(ByRef OutputVar="", Title="", Text="", Default="`n", Keystrokes="") ;http://www.autohotkey.com/forum/viewtopic.php?p=467756 { Static KeysToSend, PID, HWND, PreviousEntries If (A_ThisLabel <> "InputBox") { If HWND SetTimer, InputBox, Off If !PID { Process, Exist PID := ErrorLevel } If Keystrokes KeysToSend := Keystrokes WinGet, List, List, ahk_class #32770 ahk_pid %PID% HWND = `n0x0`n Loop %List% HWND .= List%A_Index% "`n" If InStr(Default, "`n") and (UsePrev := True) StringReplace, Default, Default, `n, , All If (Title = "") Title := SubStr(A_ScriptName, 1, InStr(A_ScriptName, ".") - 1) ": Input" SetTimer, InputBox, 20 StringReplace, Text, Text, `n, `n, UseErrorLevel InputBox, CapturedOutput, %Title%, %Text%, , , Text = "" ? 100 : 116 + ErrorLevel * 18 , , , , , % UsePrev and (t := InStr(PreviousEntries, "`r" (w := (u := InStr(Title, " - ")) ? SubStr(Title, 1, u - 1) : Title) "`n")) ? v := SubStr(PreviousEntries, t += (u ? u - 1 : StrLen(Title)) + 2, InStr(PreviousEntries "`r", "`r", 0, t) - t) : Default If !(Result := ErrorLevel) { OutputVar := CapturedOutput If t StringReplace, PreviousEntries, PreviousEntries, `r%w%`n%v%, `r%w%`n%OutputVar% Else PreviousEntries .= "`r" w "`n" OutputVar } Return Result } Else If InStr(HWND, "`n") { If !InStr(HWND, "`n" (TempHWND := WinExist("ahk_class #32770 ahk_pid " PID)) "`n") { WinDelay := A_WinDelay SetWinDelay, -1 WinSet, AlwaysOnTop, On, % "ahk_id " (HWND := TempHWND) WinActivate, ahk_id %HWND% If KeysToSend { WinWaitActive, ahk_id %HWND%, , 1 If !ErrorLevel SendInput, %KeysToSend% KeysToSend = } SetTimer, InputBox, -400 SetWinDelay, %WinDelay% } } Else If WinExist("ahk_id " HWND) { WinSet, AlwaysOnTop, On, ahk_id %HWND% SetTimer, InputBox, -400 } Else HWND = Return InputBox: Return InputBox() }
~ Created with Quick Functions for Forums by berban ~