Page 1 of 1

Exit Batch File and CMD With One Keypress Not Hitting ENTER After

Posted: 29 Apr 2022 10:55
by KEUDK
I'm trying to do one of the following:

A) Press ESC to exit the batch file and CMD without pressing enter (Preferred)
B) Press one specific key and immediately exit the batch file and CMD without having to press enter

I've tried a few things so far and the code below exits but I still have to press enter after typing "-". How can
I just press "-" by itself, not press enter, and have it exit the batch file and CMD? (I'm open to changing the key
I press to exit.)

Code: Select all

ECHO.
ECHO                 1) I like Mickey Mouse
ECHO                 2) I like Minnie Mouse
ECHO                 3) I like Donald Duck
ECHO.

SET /P M=Press 1, 2,or 3 then press ENTER 

IF %M%==1 GOTO MICKEY
IF %M%==2 GOTO MINNIE
IF %M%==3 GOTO DONALD
IF %M%==- EXIT /B
I appreciate any help!

Re: Exit Batch File and CMD With One Keypress Not Hitting ENTER After

Posted: 29 Apr 2022 11:28
by aGerman
Just take the CHOICE command.
https://ss64.com/nt/choice.html

Steffen

Re: Exit Batch File and CMD With One Keypress Not Hitting ENTER After

Posted: 29 Apr 2022 12:45
by KEUDK
I looked at that guide and others but I don't know what I'm doing wrong. How can I put that together to make ESC exit the batch file and CMD?

Re: Exit Batch File and CMD With One Keypress Not Hitting ENTER After

Posted: 29 Apr 2022 12:58
by KEUDK
I haven't been able to get it to work. I'm not sure what's going wrong but whatever way I set it up the ESC key does nothing. Can you tell me the code that'll get this to work?

Re: Exit Batch File and CMD With One Keypress Not Hitting ENTER After

Posted: 29 Apr 2022 14:36
by aGerman
Neither SET /P nor the CHOICE command supports extended keys like ESC, F1..F12, arrow keys etc. Hower, why not defining the 'e' key rather than ESC?

Steffen