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?
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?
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.
This is a pretty old question, but since the "accepted" answer is off-topic, I will post an answer to hopefully help those who accidently (or intentionally) land here.
Option 1: The "Choice" Command
DOS Command prompt window does not have a read command. You can get input from either redirecting CON or by using the CHOICE command that has been included with windows for decades:
Example
CHOICE /T 10 /C ync /CS /D y
This will wait for the user to press either y, n, or c (/C ync) If the user does not pick anything within 10 seconds (/T 10), the default, y will be chosen (/D y). You could also add a prompt with /M:
CHOICE /T 10 /C ync /CS /D y /M "Pick y/n/c (default is y in 10 seconds)"
Note that the choices are case sensitive when /CS is used. The list of choices will be appended to the end like this: "[y,n,c]", if you don't want those, make sure to include /N in your command line!
The %ERRORLEVEL% variable will store the answer, and it is base-1, meaning that returns are like this (based on example above):
Key(s) Pressed | Value of %ERRORLEVEL% variable |
---|---|
y | 1 |
n | 2 |
c | 3 |
Special Cases | Value of %ERRORLEVEL% variable |
---|---|
Error Encountered | 255 |
CTRL+C or CTRL+BREAK Pressed | 0 |
Option 2: Redirection
You can set a variable from input text by redirecting CON to a file, and then running it through a FOR command. There is no pure cmd shell way of doing this however. See here for details on how this is done. The "FOR" command was created to help with the shell's shortcomings, but can be rather difficult for others to read and can get quite complicated if the program needs to do fancy stuff.
Option 3: Third-Party Programs
If the batch is for private use, you might do this, but it isn't good to do this if your file is going to be run by others, especially on other computers. It would require you to also distribute the program, which involves getting permission, tasking the user to install it, or forcing you to write/use an install program. None of which are very friendly when you want to just share a single script.
Option 4: Other Shells
For obvious reasons, you probably won't want to do this. If you find that cmd.exe is just too limited, you might convince others to use the windows power shell (ps.exe), as it is available almost anywhere now. Keep in mind that if you do, you will be abandoning earlier versions of Windows software, and that the user may not have privileges to run it.
PowerShell is a whole other beast, so I won't get into the options you have there.
Summary
Related/See Also
Have a nice day... no actually have three. Wait, no, actually have fifty nice days! (why not!)
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.