Contents of the KEYWAIT.DOC file
KEYWAITA public domain program by Jon Saxton. Please include the
source code in any further distribution of this program.
KEYWAIT.DOSSource code for old, crude version
KEYWAIT.OS2Source code for new, refined version
KEYWAIT.EXEOS/2 protected mode executable code
KEYWAIT grew from an MSDOS program I wrote to help control AUTOEXEC.BAT
execution. It takes a numeric parameter on the command line (in the range
1 to 100) and waits that number of seconds for a keystroke. If the parameter
is omitted or is outside the legal range then KEYWAIT sets the timeout period
to 10 seconds.
If KEYWAIT detects a function key F1 to F10 then it sets the "errorlevel"
variable to a value in the range 1 to 10 corresponding to the function key.
If KEYWAIT sees the ESCape key it sets errorlevel to 27. If any other key
is pressed or the timeout period expires, KEYWAIT sets errorlevel to zero.
This means you can set up your AUTOEXEC.BAT file to do something sensible by
default whilst providing the facility to easily vary the normal start-up
process. For example:
echo Press [32mF1[33m to load [32mVMOS/3[33m.
echo Press [32mF2[33m to load [32mVM/386[33m.
echo Press [31mESC[33m to continue [31mwithout[33m DOSEDIT.
echo Press any other key (or none) to load DOSEDIT and continue.
if errorlevel 11 goto msdos
if errorlevel 2 goto vm386
if errorlevel 1 goto vmos3
rem Get here on a timeout or any keypress except F1, F2, ESC.
rem Get here on F2
rem Never return to this program
rem Get here on F1
rem Get here directly on ESC or indirectly from DOSEDIT load.
Well that was the MSDOS version. Just for want of something to do I recoded
it for OS/2. It is probably of less value under OS/2 than under MSDOS but it
may be of use to someone. It does provide a simple example of a multithread
The main program parses the command line and establishes the timeout period
before forking each of the subsidiary threads. It then waits for an event
to be caused by one of the threads.
The second thread waits on a keypress and, if it gets one, sets the program's
exit code and notifies the main program.
The third thread establishes a recurrent timer with a period of about one
second and counts down. If the count reaches zero then it tells the main
There is a small timing window which could conceivably yield the "wrong"
result when a key is hit just as the timeout period expires. This window
could be resolved via a critical section but that sort of precision is not
necessary in this program.
This program was written for text mode operation. I am using OS/2 1.0. So
far I have never even seen 1.1 and I have no idea how relevant it might be
in a Presentation Manager environment. Useless, no doubt.
Assemble with MASM 5.1 or later and link with DOSCALLS.LIB.