Ir para o conteúdo

Verificar mudança de Locks

Este programa regista as alterações dos Locks:

  • Caps Lock
  • Num Lock
  • Scroll Lock
PROGRAM LocksTest;
USES crt, windows;

(* TIPOS *)
TYPE TRegLocksState = RECORD
                      Old : record
                            Caps, Num, Scroll : boolean;
                      end;
                      Now : record
                            Caps, Num, Scroll : boolean;
                      end;
     END;

(* VARIÁVEIS *)
VAR Locks : TRegLocksState;
    ExitControl : char;


FUNCTION CapsLock : boolean;
(* Verifica Caps Lock *)
begin
     CapsLock := boolean(GetKeyState(VK_CAPITAL));
end;

FUNCTION NumLock : boolean;
(* Verifica Num Lock *)
begin
     NumLock := boolean(GetKeyState(VK_NUMLOCK));
end;

FUNCTION ScrollLock : boolean;
(* Verifica Scroll Lock *)
begin
     ScrollLock := boolean(GetKeyState(VK_SCROLL));
end;


PROCEDURE RefreshLocks(const OnLoad : boolean);
(* Actualiza indicadores de Locks *)

   function Bool2OnOff(const b : boolean) : string;
   (* Converte o booleano do indicador num indicador ON/OFF *)
   begin
        case b of
             true  : Bool2Onoff := 'ON ';
             false : Bool2Onoff := 'OFF';
        end;
   end;

begin
     // Indicador Caps Lock
     Locks.Now.Caps := CapsLock;
     if (Locks.Now.Caps <> Locks.Old.Caps) or OnLoad then begin
        GotoXY(1,1);
        Write('  Caps Lock   ', Bool2OnOff(Locks.Now.Caps));
        Locks.Old.Caps := Locks.Now.Caps;
     end;

     // Indicador Num Lock
     Locks.Now.Num := NumLock;
     if (Locks.Now.Num <> Locks.Old.Num) or OnLoad then begin
        GotoXY(1,2);
        Write('   Num Lock   ', Bool2OnOff(Locks.Now.Num));
        Locks.Old.Num := Locks.Now.Num;
     end;

     // Indicador Scroll Lock
     Locks.Now.Scroll := ScrollLock;
     if (Locks.Now.Scroll <> Locks.Old.Scroll) or OnLoad then begin
        GotoXY(1,3);
        Write('Scroll Lock   ', Bool2OnOff(Locks.Now.Scroll));
        Locks.Old.Scroll := Locks.Now.Scroll;
     end;
end;


BEGIN  (* BLOCO PRINCIPAL *)
     RefreshLocks(true); // Mete indicadores: está a iniciar programa - parâmetro deve ser TRUE

     repeat
           while not KeyPressed do begin
                 RefreshLocks(false);  // Actualiza
                 Delay(100);
           end;

           ExitControl := ReadKey;
     until (ExitControl = #27);  // Sai no ESC
END.

Por thoga31. Snippet optimizado por nunopicado.