a
MOV AH,01
INT 16
JNZ 0108
XOR AL,AL
MOV AH,4C
INT 21
rcx
0c
nkbhit.com
w
q
a
XOR AX,AX
INT 16
MOV AH,4C
INT 21
rcx
8
ngetch.com
w
q
You can create the .COM files by putting the above scripts into KBHIT.SRC and GETCH.SRC, then issuing these commands:
debug kbhit.com < kbhit.src
and
debug getch.com < getch.src
which will invoke DEBUG, create the files, run the script, and put the resulting binary code into the files before terminating. The blank line above "rcx" is an essential part of the script - do not omit it. @echo off
echo a > getch.src
echo XOR AX,AX >> getch.src
echo INT 16 >> getch.src
echo MOV AH,4C >> getch.src
echo INT 21 >> getch.src
echo. >> getch.src
echo rcx >> getch.src
echo 8 >> getch.src
echo ngetch.com >> getch.src
echo w >> getch.src
echo q >> getch.src
debug < getch.src
echo Press keys to see what happens: the program recognizes 'A, 'a', 'g', 'Space, and Enter - ESC to terminate.
echo Note that the program is case sensitive and does not recognize certain keys on the enhanced keyboard.
:loop
getch
if errorlevel 65 if not errorlevel 66 echo A
if errorlevel 97 if not errorlevel 98 echo a
if errorlevel 103 if not errorlevel 104 echo g
if errorlevel 13 if not errorlevel 14 echo Enter
if errorlevel 32 if not errorlevel 33 echo Space bar
if errorlevel 27 if not errorlevel 28 goto done
goto loop
:done
del getch.*
For one-shot use, it is not necessary to create the .COM file unless there is an exit code (ERRORLEVEL) to be tested - these examples all require the exit code and so all require the .COM file. @echo off
echo a > kbhit.src
echo MOV AH,01 >> kbhit.src
echo INT 16 >> kbhit.src
echo JNZ 0108 >> kbhit.src
echo XOR AL,AL >> kbhit.src
echo MOV AH,4C >> kbhit.src
echo INT 21 >> kbhit.src
echo. >> kbhit.src
echo rcx >> kbhit.src
echo 0c >> kbhit.src
echo nkbhit.com >> kbhit.src
echo w >> kbhit.src
echo q >> kbhit.src
debug < kbhit.src > nul
:loop
kbhit
if not errorlevel 1 goto loop
if errorlevel 65 if not errorlevel 91 echo Upper case letter
if errorlevel 97 if not errorlevel 123 echo Lower case letter
if errorlevel 1 if not errorlevel 65 echo Something other than a letter
if errorlevel 91 if not errorlevel 97 echo Something other than a letter
if errorlevel 123 echo Something other than a letter
del kbhit.*
To adapt those programs to your own needs, you will need a list of keys and their codes. Unfortunately this is more of a task than I want to undertake at this time. I have made a list of ASCII characters and their codes available, but that doesn't address such things as the arrow keys and function keys. For these, you will need a list of scan codes (as distinct from character codes) and examples of how to deal with scan codes. Unfortunately, that will have to wait for another rainy weekend. @echo off
:: Author=TDavis, Status=Public Domain
set drive=Invalid
echo a > testdrv.src
echo mov ax,4408 >> testdrv.src
echo mov bl,%1 >> testdrv.src
echo int 21 >> testdrv.src
echo mov ah,4c >> testdrv.src
echo int 21 >> testdrv.src
echo. >> testdrv.src
echo rcx >> testdrv.src
echo 0b >> testdrv.src
echo w >> testdrv.src
echo q >> testdrv.src
debug testdrv.com < testdrv.src > nul
testdrv
if errorlevel 15 goto cont0
set drive=removable
if errorlevel 1 set drive=non%drive%
:cont0
echo Drive %1 is %drive%
:end
The program is invoked with the drive number (0 = default, A = 1, B = 2, etc.) and reports whether the drive is invalid, removable media, or non-removable media. It does not hang if the removable drive is empty, does not issue error messages, and works with network and CD drives, and with real DOS, Win9x, and NT. Be aware that in the form presented, it writes to the default drive:directory and does not clean up after itself (I left the drive variable and the script and com files so that users can examine them. A working version should clean up after itself. ** Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001 Ted Davis - see License, included by reference. **
Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.
Back to the Table of Contents page
Back to my personal links page - back to my home page