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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
KEUDK
Posts: 3
Joined: 29 Apr 2022 10:44

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

#1 Post by KEUDK » 29 Apr 2022 10:55

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!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 29 Apr 2022 11:28

Just take the CHOICE command.
https://ss64.com/nt/choice.html

Steffen

KEUDK
Posts: 3
Joined: 29 Apr 2022 10:44

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

#3 Post by KEUDK » 29 Apr 2022 12:45

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?

KEUDK
Posts: 3
Joined: 29 Apr 2022 10:44

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

#4 Post by KEUDK » 29 Apr 2022 12:58

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#5 Post by aGerman » 29 Apr 2022 14:36

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

Post Reply