| Index | Manual | Download | Source | Macros | Embedded word Case ins.
Sitemap | Links | Forum | Tests | Projects

# locktest.htm
Header
Dos
Win

Lock test is dual 16-bit Dos and 32-bit Windows executable utility which examines status of the keyboard indicators NumLock, CapsLock, ScrollLock, and returns the detected status in errorlevel and as a digit `0` or `1` on the standard output.

An argument should start with uppercase or lowercase letter `N`,`C` or `S`, it may be optionaly prefixed with slash `/` or dash `-`. For instance `locktest Scroll`, `locktest /scrollLock`, `locktest -s`.

This utility displays help text when wrong argument was provided.

> Lock Test can be used in batch scripts for instance to warn the user that keyboard is in unexpected state, or to control the running PC service with *Lock keyboard keys, without having the monitor switched on.

Dual utility means that the executable file locktest.exe works both in 16-bit Dos and in 32-bit or 64-bit Windows. The Dos version is compiled to a temporary MZ file locktesd.exe and then it will be embedded to the Windows version , i. e. to PE file locktest.exe as its stub. Dos stub is used by the loader when Windows subsystem is not available.

Both versions have the same functionality, their similar parts are shared via Header division.

Format
  Dual Dos and Windows console application.
Platform
  Dos and Windows.
Input
  Identification of investigated keyboard lock is specified as an argument `Num`, `Caps` or `Scroll`.
Output
  Digit `0` and errorlevel=0 when the lock is off,
  digit `1` and errorlevel=1 when the lock is on (its LED is shining).
Build
  `euroasm locktest.htm`
Run
  `locktest.exe`

Header
  This division defines common preprocessing variable %HelpText which is shared between Dos and Windows versions with minor difference between their program formats (MZ and PE) represented by %%^FORMAT. Expansion of %%^FORMAT variable is postponed to %HelpExp.

              EUROASM UNICODE=OFF, AUTOSEGMENT=ON
    %HelpText %SET 13,10,                                                         \
    'Program:  locktest.exe version %^DATE.',13,10,                               \
    'Licence:  Public domain by vitsoft.',13,10,                                  \
    'Format:   %%^FORMAT (dual Dos | Windows console application).',13,10,        \
    'Function: Test NumLock | CapsLock | ScrollLock keyboard status.',13,10,      \
    'Parameter:Num | Caps | Scroll, optionally prefixed with dash | slash.',13,10,\
    'Output:   Digit 0 | 1, errorlevel 0 | 1.',13,10,                             \
    'Example:  locktest ScrollLock   or  locktest -s',13,10,                      \
     0

Dos version
  This 16-bit MZ executable will be linked to Windows version as a stub file. It can also be tested as locktesd.exe in real DOS or in NTVDM emulation from 32bit MS Windows.

    locktesd PROGRAM FORMAT=MZ, MODEL=SMALL, WIDTH=16, Entry=Main:
             INCLUDE1 dosapi.htm, cpuext.htm
    %HelpExp %SET2 %HelpText     ; Delayed expansion of %^FORMAT variable.
    HelpText DB %HelpExp         ; Final definition of HelpText in MZ variant.
    Digit    DB '0'              ; Output character 0 or 1.
    Main:    PROC                ; Entry point of Dos version.
              PUSH PARA#HelpText ; [DATA] will be addressed with DS.
              POP DS             ; [PSP] stays adressed with ES.
              GetArg 1           ; Use Dos GetArg macro to get 1st argument to ES:SI,CX.
              JC Help:           ; Abort if none was found.
    .0x2D:    ; Labels .0x2D: | .0x2F: are used when argument starts with dash | slash.
    .0x2F:    SEGES:LODSB        ; The first character should be one of / - N C S n c s.
              OR AL,0x20         ; Convert letters N C S to n c s.
              Dispatch AL,0x2D,0x2F,'n','c','s' ; Use macro Dispatch to fork the thread.
    Help:     StdOutput HelpText ; Display HelpText when the argument is not valid.
              TerminateProgram Errorlevel=8
    .c:       MOV BL,0x40        ; CapsLock Flag mask.
              JMP Test:
    .s:       MOV BL,0x10        ; ScrollLock Flag mask.
              JMP Test:
    .n:       MOV BL,0x20        ; NumLock Flag mask.
    Test:     DosAPI INT=16h,AH=2; Returns keyboard flags in AL (in fact it's BiosAPI).
              AND AL,BL          ; Test the requested lock flag.
              JZ ReturnAL:       ; Skip when the flag is reset.
              MOV AL,1           ; Prepare output errorlevel and digit.
    ReturnAL: OR [Digit],AL      ; AL is now 0 or 1.
              StdOutput Digit, Size=1, Eol=Yes ; Write 0 or 1 on StdOutput.
              TerminateProgram Errorlevel=AL   ; Terminate with errorlevel 0 or 1.
             ENDPROC Main:
            ENDPROGRAM locktesd

Win version
  This is standard MS Windows console utility, which links previously compiled Dos version locktesd.exe as its MZ stub.
  It begins with pseudoinstruction %DROPMACRO which makes €ASM to forget homonymous 16bit macros `GetArg, StdOutput, TerminateProgram` defined by the inclusion of dosapi.htm in the previous program locktesd. Otherwise €ASM would complain with `W2512 Overwriting macro "GetArg" previously defined at "dosapi.htm"{326}.`.

             %DROPMACRO *    ; Forget definitions of homonymous macros from dosapi.htm.
    locktest PROGRAM FORMAT=PE, WIDTH=32,ENTRY=Main:, \
                     ICONFILE=, STUBFILE="locktesd.exe"
             INCLUDE1 winapi.htm, winsgui.htm, cpuext.htm
             IMPORT GetKeyState,LIB=user32.dll
    %HelpExp %SET2 %HelpText ; Delayed expansion of %^FORMAT variable.
    HelpText DB %HelpExp     ; Final definition of HelpText in PE variant.
    Digit    DB '0'          ; Output character 0 or 1.
    Main:    PROC            ; Entry point of Win version.
              GetArg 1       ; Use Win GetArg macro to get 1st argument to ESI,ECX.
              JC Help:       ; Abort if none was found.
    .0x2D:                   ; Labels .0x2D: | .0x2F: are used when argument starts with dash | slash.
    .0x2F:    LODSB          ; The first character should be one of / - N C S n c s.
              OR AL,0x20     ; Convert letters N C S to n c s.
              Dispatch AL,0x2D,0x2F,'n','c','s'
    Help:     StdOutput HelpText ; Display HelpText when the argument is not valid.
              TerminateProgram Errorlevel=8
    .c:       MOV EAX,VK_CAPITAL
              JMP Test:
    .s:       MOV EAX,VK_SCROLL
              JMP Test:
    .n:       MOV EAX,VK_NUMLOCK
    Test:     WinAPI GetKeyState, EAX         ; Return lock state of virtual key EAX.
              AND EAX,1                       ; Isolate the LSbit.
              OR [Digit],AL                   ; Modify the digit for StdOutput.
              StdOutput Digit, Size=1,Eol=Yes ; Write 0 or 1 on StdOut.
              TerminateProgram Errorlevel=EAX ; Terminate with errorlevel 0 or 1.
             ENDPROC Main:
            ENDPROGRAM locktest

▲Back to the top▲