Blockerwiz
Blockerwiz

Reputation: 31

Detecting a keyboard input in batch

In a batch file im messing with, I want the program to goto a certain section after pressing a certain key, such as D. Is there any way to do this after pressing a key without also having to press enter? I'm sorta a beginner.

Upvotes: 3

Views: 3428

Answers (1)

Vyren
Vyren

Reputation: 358

This is possible using the choice command. This command will wait for a keypress, and if the key is in the list of keys after /c then it returns the offset of the key pressed in the list of keys.

choice /c AB /m "Press A or B"
if errorlevel 2 goto optionb
if errorlevel 1 goto optiona

You can also use the optional /n arguement to hide the list of options ([A,B] in this case) that appears after the message text.

Upvotes: 4

Related Questions