如何在 AutoHotkey 中提示用户输入?

How do I prompt for user input in AutoHotkey?

我正在编写一个 AutoHotkey 脚本,它需要将键入的用户输入检索为字符串。 (用户名、密码、文件名等)

如何使用 AutoHotkey 检索用户输入?

您可以使用 AutoHotkey 的内置 InputBox

InputBox, UserInput, Enter Name, Please enter a username:, , 300, 150
MsgBox, You entered %UserInput% as your username


以下是 InputBox documentation

的节选

InputBox

Displays an input box to ask the user to enter a string.

InputBox, OutputVar [, Title, Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default]

Remarks

The dialog allows the user to enter text and then press OK or CANCEL. The user can resize the dialog window by dragging its borders.

Example

InputBox, password, Enter Password, (your input will be hidden), hide 
InputBox, UserInput, Phone Number, Please enter a phone number., , 640, 480
if ErrorLevel
    MsgBox, CANCEL was pressed.
else
    MsgBox, You entered "%UserInput%"

来源: Autohotkey documentation for InputBox

对于用户名和密码,您可以使用 Stevoisiak 提供的内容。但是对于文件名,要求用户输入文件路径是相当残酷的。

最好使用 FileSelectFile or FileSelectFolder(它是带示例的官方文档链接)。

另外,如果你需要问几个项目,而不是顺序显示单独的输入框,最好做一个gui。幸运的是,这在 AutoHotkey 中并不麻烦:

    Gui Add, Text, xm section, login
    Gui Add, Edit, ys x100 W300 vlogin, %defaultLogin%
    Gui Add, Text, xm section, password
    Gui Add, Edit, ys x100 W300 Password vpassword
    Gui Add, Text, xm section, File: 
    Gui Add, Edit, ys x100 W300 vusrSelFile
    Gui Add, Button, ys, Browse
    Gui Add, Button, section Default, OK
    Gui Add, Button, ys gExit, Cancel
    Gui Show
Exit

ButtonOK:
    Gui Submit
    ;Gui Submit, NoHide if you wanna check contrains and let user fix their input
    MsgBox,
        (LTrim
        login: %login%
        password: %password%
        file: %usrSelFile%
        )
ExitApp
        
    
ButtonBrowse:
    FileSelectFile fPath
    GuiControl,, usrSelFile, %fPath%
return

GuiClose:
Exit:
    ExitApp

GuiDropFiles: ; you can also let users drop files on the GUI window
Loop Parse, A_GuiEvent, `n
{
    GuiControl,, usrSelFile, %A_LoopField%
    return ; only first dropped file selected, others ignored
}
return ; in case the event been triggered with no files in list