Board index » delphi » How to track NUM and CAPS lock

How to track NUM and CAPS lock

I have Delphi 2 and I would like to know how to track the state of the NUM and
CAPS lock keys.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

 

Re:How to track NUM and CAPS lock


This code is not elegant, but it works.  It assumes you have a form with a
Memo and a statusbar:

procedure TForm1.FormCreate(Sender: TObject);
begin
  if GetKeyState(vk_Capital) and $01 = 1 then
    StatusBar1.Panels.Items[1].Text := 'CAP';
  if GetKeyState(vk_NumLock) and $01 = 1 then
    StatusBar1.Panels.Items[2].Text := 'NUM';
  if GetKeyState(vk_Scroll) and $01 = 1 then
    StatusBar1.Panels.Items[3].Text := 'SCRL';
end;

function GetKey(K : Word) : String;
begin
  case K of
    vk_Capital  :  Result := 'vk_Capital';
    vk_NumLock  :  Result := 'vk_NumLock';
    vk_Scroll   :  Result := 'vk_Scroll';
  else
    Result := Chr(K);
  end;
end;

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if GetKey(Key) = 'vk_Capital' then begin
    if StatusBar1.Panels.Items[1].Text = 'CAP' then
      StatusBar1.Panels.Items[1].Text := ' '
    else
      StatusBar1.Panels.Items[1].Text := 'CAP';
  end;
  if GetKey(Key) = 'vk_NumLock' then begin
    if StatusBar1.Panels.Items[2].Text = 'NUM' then
      StatusBar1.Panels.Items[2].Text := ' '
    else
      StatusBar1.Panels.Items[2].Text := 'NUM';
  end;
  if GetKey(Key) = 'vk_Scroll' then begin
    if StatusBar1.Panels.Items[3].Text = 'SCRL' then
      StatusBar1.Panels.Items[3].Text := ' '
    else
      StatusBar1.Panels.Items[3].Text := 'SCRL';
  end;
end;

Quote
heric...@hem1.passagen.se wrote in message

<6f3cv2$fk...@nnrp1.dejanews.com>...
Quote
>I have Delphi 2 and I would like to know how to track the state of the NUM
and
>CAPS lock keys.

>-----== Posted via Deja News, The Leader in Internet Discussion ==-----
>http://www.dejanews.com/   Now offering spam-free web-based newsreading

Re:How to track NUM and CAPS lock


In article <6f3cv2$fk...@nnrp1.dejanews.com>, heric...@hem1.passagen.se
says...

Quote
> I have Delphi 2 and I would like to know how to track the state of the NUM and
> CAPS lock keys.

procedure TForm1.CheckCapsLock;
begin
        if Odd(GetKeyState(VK_CAPITAL)) then
                // CapsLock on code here
        else
                // CapsLock off code here
end;  

procedure TForm1.CheckNumLock;
begin
        if Odd(GetKeyState(VK_NUMLOCK)) then
                // NumLock on code here
        else
                // NumLock off code here
end;

--
Carlos
ctre@dont_spam_me.cobansa.com

Other Threads