Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prompt for user input in AutoHotkey?

I'm writing an AutoHotkey script which needs to retrieve typed user input as a string. (Username, password, filename, etc.)

How do I retrieve user input with AutoHotkey?

like image 947
Stevoisiak Avatar asked Jan 30 '23 21:01

Stevoisiak


2 Answers

You can use AutoHotkey's built in InputBox.

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

Input box for a username


Here's an excerpt from the 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%"

Source: Autohotkey documentation for InputBox

like image 192
Stevoisiak Avatar answered Feb 01 '23 09:02

Stevoisiak


For username and password you can go with what Stevoisiak offered. but for filename, it's rather cruel to require users to enter filepath.

Better go with FileSelectFile or FileSelectFolder (it's official documentation links with examples).

Also, if you need to ask several items, instead of displaying separate input boxes sequentially, it's much better to make a gui. Fortunately, it's not burdensome in 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
like image 30
LogicDaemon Avatar answered Feb 01 '23 11:02

LogicDaemon