I used to have a little utility program for this, but I can't find it
now. I seem to recall it was one of those little debug-created
programs that called some interrupt or read a BIOS location to get the
state of the shift keys and returned that as an exit code which could
be tested with errorlevel.
Any pointers?
Thanks
tbone
This will create KBFLAG.COM and there's a simple example.
KBFLAG -- by Nico Mark -- from PC Magazine, December 23, 1986
KBFLAG can be used to cause branching in batch files, so that execution of
different parts of the file can be dependent on the state of toggle and
shift
keys. You can, for example, abort execution of AUTOEXEC.BAT if the CapsLock
key has been toggled while CONFIG.SYS in running.
KBFLAG tests for a keystroke in the buffer, and sets errorlevel to the
value of
the key's KBFLAG in the ROM BIOS. Thus, if the Ins key has been toggled, it
will return an errorlevel of 128. Other values are:
1 = Right Shift
2 = Left Shift
4 = Ctrl key
8 = Alt key
16 = ScrollLock
32 = NumLock
64 = CapsLock
128 = Ins
(You can use sums of these values to correspond to combinations of keys, so
96 = CapsLock and NumLock together.)
If you put these lines at the start of autoexec.bat--
KBFLAG
IF ERRORLEVEL 64 GOTO :END
--and put the label :END at the end of the file, autoexec.bat will then
check
to see if CapsLock has been pressed, and will jump the end of the batch
if it
has. To prevent autoexec.bat from executing on bootup, simply hit CapsLock
while config.sys is running.
You can use variations of this technique to cause different sets of programs
to run during autoexec.bat (or any batch file). For example, Caps Lock
could
cause only a few programs to run; Alt + CapsLock could cause others; etc.
@echo off
Del /f /q /a t.dat >nul 2>&1
For %%b In (
"B840008ED8A01700B44CCD21"
) Do >>t.dat (Echo.For b=1 To len^(%%b^) Step 2
Echo WScript.StdOut.Write Chr^(Clng^("&H"^&Mid^(%%b,b,2^)^)^) : Next)
Cscript /b /e:vbs t.dat > "KBFLAG.COM"
Del t.dat >nul 2>&1
@echo off
:loop
kbflag
if %errorlevel% EQU 1 echo Right Shift
if %errorlevel% EQU 2 echo Left Shift
if %errorlevel% EQU 4 echo Ctrl key
if %errorlevel% EQU 8 echo Alt key
if %errorlevel% EQU 16 echo ScrollLock
if %errorlevel% EQU 32 echo NumLock
if %errorlevel% EQU 64 echo CapsLock
if %errorlevel% EQU 128 echo Ins
goto :loop
pause
--
Regards,
Mic
>This will create KBFLAG.COM and there's a simple example.
Unfortunately, this is only part way there. It appears to handle keys
like Control as keystrokes, and not shift keys; i.e. keys that have a
state of either pressed or not pressed. Perhaps this is due to running
under Windows XP...
I expanded the sample batch file thus:
:loop
kbflags
set ec=%errorlevel%
set el=%ec%
set desc=
if %ec% == 0 set desc=None
if %ec% gtr 127 set desc=%desc%+Ins
if %ec% gtr 127 set /a ec=ec-128
if %ec% gtr 63 set desc=%desc%+CapsLock
if %ec% gtr 63 set /a ec=ec-64
if %ec% gtr 31 set desc=%desc%+NumLock
if %ec% gtr 31 set /a ec=ec-32
if %ec% gtr 15 set desc=%desc%+ScrollLock
if %ec% gtr 15 set /a ec=ec-16
if %ec% gtr 7 set desc=%desc%+Alt
if %ec% gtr 7 set /a ec=ec-8
if %ec% gtr 3 set desc=%desc%+Ctrl
if %ec% gtr 3 set /a ec=ec-4
if %ec% gtr 1 set desc=%desc%+LeftShift
if %ec% gtr 1 set /a ec=ec-2
if %ec% gtr 0 set desc=%desc%+RightShift
echo errorlevel %el%: %desc%
goto :loop
This shows me all the combinations that can be detected. Indeed, I see
lines like:
errorlevel 100: +CapsLock+NumLock+Ctrl
errorlevel 96: +CapsLock+NumLock
errorlevel 100: +CapsLock+NumLock+Ctrl
errorlevel 96: +CapsLock+NumLock
CapsLock and NumLock are being treated as expected - they have states
that are latched, if you will. But what you see above is not me
pressing the Ctrl key twice, it's me holding the control key down. So
every other loop sees the control key being "pressed" again,
presumably due to the keyboard repeat.
What I'd like is a way to take a peek at the instantaneous state of
any of the shift keys.
thx
tbone