Ativando/Desativando as teclas CAPSLOCK, NUMLOCK e SCROLLLOCK

 

Podemos mostrar no StatusBar quando as teclas CAPSLOCK, NUMLOCK e SCROLLLOCK estejam ativadas ou desativadas. Para isso, basta verificar uma função de API chamada GetKeyState (passando como parâmetro a tecla desejada), e verificar o seu estado (um número é retornado, par quando a tecla esta ativada e ímpar para desativada). Podemos utilizar o seguinte código no evento OnKeyDown do formulário:

 

{ tecla NUMLOCK }

if odd (GetKeyState(VK_NUMLOCK)) then

  StatusBar1.Panels[0].Text := 'NUM'

else

  StatusBar1.Panels[0].Text := '';

{ tecla CAPSLOCK }

if odd (GetKeyState(VK_CAPITAL)) then

  StatusBar1.Panels[1].Text := 'CAPS LOCK'

else

  StatusBar1.Panels[1].Text := '';

{ tecla SCROLLLOCK }

if odd (GetKeyState(VK_SCROLL)) then

  StatusBar1.Panels[2].Text := 'SCROLL LOCK'

else

  StatusBar1.Panels[2].Text := '';

 

Altere para True a propriedade KeyPreview do formulário. Para saber o código das outras teclas procure por Virtual key codes no Help do Delphi. Rode a aplicação e ative / desative a tecla para testar (Figura).

teclas.gif

 

Luciano Pimenta®

Editor do Portal ClubeDelphi.Net

webeditor@clubedelphi.net

www.clubedelphi.net