Batch file capabilities may be increased with the aid of auxiliary programs, some of wich may be very simple if they are written in assembly language:
@ECHO OFF
(
ECHO A100
ECHO MOV AH,B
ECHO INT 21
ECHO MOV AH,4C
ECHO INT 21
ECHO/
ECHO RCX
ECHO 8
ECHO W
ECHO Q
) | DEBUG CHKKEY.COM
Previous Batch file creates the 8-bytes long CHKKEY.COM auxiliary program that check if a key was pressed and return an ERRORLEVEL of 255 if so, or zero if not. For example:
:waitforkey
echo Waiting for a key to be pressed...
chkkey
if not errorlevel 1 goto waitforkey
echo A key was pressed!
If you have not the DEBUG.COM program, you may get it in the web. This way, to wait for a key for 5 seconds:
for /F "tokens=3 delims=:." %%a in ("%time%") do set /A second=%%c+5
if %second% geq 60 set /A second-=60
:waitforkey
for /F "tokens=3 delims=:." %%a in ("%time%") do if %%c == %second% goto timeexceeded
chkkey
if not errorlevel 1 goto waitforkey
set /P command=
If you change the B value by 1 in MOV AH,B instruction, a key is read and its ASCII code is returned in ERRORLEVEL; this feature allows to read a single keystroke and process it immediately. If the value is 8, the key read is not displayed in the screen; this allows to process any key of the keyboard even function and special keys that return two values: the first one is zero (that identify a special key) and the second one identify the key pressed. For example, F1 key returns 0 and 59.