How to check if a key was pressed in Batch?

2019-09-18 05:31发布

问题:

I just need a Batch command that can check if some key was pressed, or a function that will return the keyboard key from the buffer.

Where can I find/download such a function?

回答1:

read by default lets you wait until the user presses enter. It takes an optional parameter which is the name of a variable in which it'll save the user input (everything up to the newline).

eivanec@sisifo ~ $ read test
hello
eivanec@sisifo ~ $ echo $test
hello

You can also specify a different character to wait for (instead of newline) with the -d switch.



回答2:

In this topic the GETKEY.COM auxiliary program is described. It read a key and returns its ASCII code via ERRORLEVEL, so it may be processed in any way. Even special and function keys may be managed this way.

Let me know if you need further details on GETKEY usage.