Skip to main content

How to Read Keyboard Input Without Requiring "Enter" in Batch Script

The standard SET /P command is great for getting a string of text from a user, but it has a major drawback for simple interactions: it always requires the user to press the Enter key to submit their input. For a simple "Yes/No" prompt or a menu selection, this feels clumsy. What you really want is a way to capture a single keypress the moment it happens.

This is the job of the CHOICE command. It is a powerful, built-in utility specifically designed to wait for a user to press one key from a predefined set of choices, making your scripts far more responsive and professional.

The Challenge: SET /P Requires the Enter Key

A simple prompt with SET /P looks like this: SET /P "Confirm=Are you sure (Y/N)? " The user must type Y and then press Enter. For quick confirmations, this extra keystroke is inefficient. We need a command that reacts instantly.

The Core Command: CHOICE

The CHOICE command pauses the script and waits for the user to press a key from a list of characters you specify. It does not wait for the Enter key.

Its most important feature is how it communicates the user's selection back to the script: it sets the %ERRORLEVEL% variable to a number representing the position of the chosen key in your list.

Basic Example: A Simple "Yes/No" Prompt

This script asks a "Yes/No" question and immediately acts on the user's single keypress.

@ECHO OFF
ECHO WARNING: This will delete temporary files.

CHOICE /C YN /M "Are you sure you want to continue?"

REM --- This is the critical part: Check ERRORLEVEL from HIGHEST to LOWEST ---
IF ERRORLEVEL 2 (
ECHO You pressed 'N'. Operation cancelled.
GOTO :End
)
IF ERRORLEVEL 1 (
ECHO You pressed 'Y'. Deleting files...
REM DEL C:\Temp\*.*
)

:End

How CHOICE Works: The ERRORLEVEL Index

The CHOICE command creates a 1-based index from the characters you provide in the /C switch.

If you use CHOICE /C YN, the mapping is:

  • User presses Y -> %ERRORLEVEL% is set to 1 (first in the list).
  • User presses N -> %ERRORLEVEL% is set to 2 (second in the list).

If you use CHOICE /C ABC, the mapping is:

  • User presses A -> %ERRORLEVEL% is 1.
  • User presses B -> %ERRORLEVEL% is 2.
  • User presses C -> %ERRORLEVEL% is 3.

Key CHOICE Parameters Explained

CHOICE has several switches that make it very flexible:

  • /C <Choices>: (Required) Specifies the list of valid keys the user can press (e.g., /C YNC for Yes/No/Cancel).
  • /M <Message>: Displays a Message string as the prompt.
  • /N: No prompt. This hides the default [Y,N]? text, allowing you to create a completely custom prompt with ECHO.
  • /T <Timeout>: Timeout in seconds. If the user doesn't press a key within this time, a default choice is made.
  • /D <Default>: Default choice to be used with /T. Must be a character from your /C list.

Example with Timeout

REM Waits 10 seconds. If no key is pressed, it automatically chooses 'N'.
CHOICE /C YN /M "Proceed with update?" /T 10 /D N

Common Pitfalls and How to Solve Them

The Critical IF ERRORLEVEL Order

This is the most common mistake when using CHOICE. The command IF ERRORLEVEL N means "if the error level is N or greater." Because of this, you must check for the highest possible value first and work your way down.

Example of script with error (wrong order):

CHOICE /C YN
REM This logic is FLAWED.
IF ERRORLEVEL 1 ECHO You chose Y.
IF ERRORLEVEL 2 ECHO You chose N.

If the user presses N, ERRORLEVEL becomes 2. The first IF (is it 1 or greater?) is TRUE, so it will incorrectly print "You chose Y" and stop.

Solution (CORRECT ORDER)

CHOICE /C YN
REM Always check from HIGHEST to LOWEST.
IF ERRORLEVEL 2 ECHO You chose N.
IF ERRORLEVEL 1 ECHO You chose Y.

Alternatively, you can use a more precise comparison: IF "%ERRORLEVEL%"=="1" ECHO You chose Y.

Handling the "Abort" Key (Ctrl+C)

If the user presses Ctrl+C to abort the choice, the CHOICE command will set the %ERRORLEVEL% to 0. A robust script should check for this.

CHOICE /C YN
IF ERRORLEVEL 2 GOTO :ChoiceN
IF ERRORLEVEL 1 GOTO :ChoiceY
IF ERRORLEVEL 0 GOTO :Aborted

:Aborted
ECHO User aborted the script.

Practical Example: A Multi-Option Menu

This script presents a menu and uses CHOICE to let the user select an action with a single keypress.

@ECHO OFF
:Menu
CLS
ECHO --- System Maintenance Menu ---
ECHO.
ECHO 1. Run Backup
ECHO 2. Check Disk Space
ECHO 3. Quit
ECHO.

REM /N hides the default prompt, so our menu looks clean.
CHOICE /C 123 /N /M "Please enter your choice [1,2,3]: "

REM Check ERRORLEVEL from highest to lowest.
IF ERRORLEVEL 3 GOTO :Quit
IF ERRORLEVEL 2 GOTO :CheckDisk
IF ERRORLEVEL 1 GOTO :Backup

:Backup
ECHO Running backup...
PAUSE
GOTO :Menu

:CheckDisk
ECHO Checking disk space...
PAUSE
GOTO :Menu

:Quit
ECHO Exiting.

Conclusion

The CHOICE command is the standard and most effective tool for capturing immediate, single-key input from a user in a batch script. It is far superior to SET /P for creating responsive menus and confirmation prompts.

Key takeaways for using CHOICE:

  • It uses %ERRORLEVEL% to return the user's selection based on a 1-based index of your choice list.
  • You must check ERRORLEVEL from the highest value to the lowest (IF ERRORLEVEL N ...).
  • Use the /N, /M, and /T switches to customize the prompt and add timeouts.
  • Always check for ERRORLEVEL 0 to gracefully handle cases where the user aborts the script.