root
root

Reputation: 343

Automatically accept user input Windows Batch

I have a batch file that loads on startup that presents the user with a "menu" of applications they can choose to load by typing the corresponding "menu number". How can we reduce user input, simplifying their selection, to automatically pass any key pressed to the command prompt without needing the Enter key to be pressed after their selection is typed? That is, if a user inputs 1, read that value as if the user had input 1 + Enter The constant here is that the menu only spans from 1-9, so the input from the user will ever only be 1 character long (if that's at all relevant).

In brief how the menu is presented and how user input is handled:

:menu
echo [Menu]
echo.
echo [1] - PuTTy
echo [2] - Chrome (Google)
echo.
echo [0] - Exit

SET /P M=(:
IF %M%==1 goto choice1
IF %M%==2 goto choice2
IF %M%==0 goto choice0

:choice1
start "" "%ProgramFiles(x86)%\PuTTy\putty.exe"
goto menu

:choice2
start "" "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" www.google.com
goto menu

:choice0
exit

Upvotes: 2

Views: 1045

Answers (1)

wimh
wimh

Reputation: 15232

You can use Choice for this. So your script will look similar to:

:menu
echo [Menu]
echo.
echo [1] - PuTTy
echo [2] - Chrome (Google)
echo.
echo [0] - Exit

choice /c 120 /n /m (:

IF errorlevel 3 goto choice0
IF errorlevel 2 goto choice2
IF errorlevel 1 goto choice1

:choice1
start "" "%ProgramFiles(x86)%\PuTTy\putty.exe"
goto menu

:choice2
start "" "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" www.google.com
goto menu

:choice0

Upvotes: 2

Related Questions