Toggle the NUM LOCK, CAPS LOCK, and SCROLL LOCK Keys

'

This article demonstrates how to toggle the NUM LOCK, CAPS LOCK, AND SCROLL LOCK keys under Windows 95, Windows 98, Windows NT, or Windows 2000.

To toggle the NUM LOCK, CAPS LOCK, or SCROLL LOCK keys, you can use the following logic:
Use the GetKeyboardState function to determine the state of the key.
Determine which operating system is being used with the GetVersionEx API. (Windows 95/98 and Windows NT/2000 require different methods for toggling these keys.)
Under Windows 95 or Windows 98, use the SetKeyboardState API function to set the state of the key. Under Windows NT or Windows 2000, use the keybd_event function to simulate a key press.
This example shows how to toggle these three keys to "on" if they are "off." This sample may be easily modified to toggle them off or just to check their state.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
 o : OSVERSIONINFO;
 NumLockState : boolean;
 ScrollLockState : boolean;
 CapsLockState : boolean;
 keys : TKeyboardState;
begin
 o.dwOSVersionInfoSize := SizeOf(o);
 GetVersionEx(o);
 GetKeyboardState(keys);

 //NumLock handling:
 NumLockState := ( keys[VK_NUMLOCK] > 0);
 If NumLockState <> True then    //Turn numlock on
  If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  //=== Win95/98
     begin
          keys[VK_NUMLOCK] := 1;
          SetKeyboardState(keys);
     end
        Else
          If o.dwPlatformId = VER_PLATFORM_WIN32_NT Then //=== WinNT
       begin
        //Simulate Key Press
          keybd_event(VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY Or 0, 0);
        //Simulate Key Release
          keybd_event (VK_NUMLOCK, $45, KEYEVENTF_EXTENDEDKEY
           Or KEYEVENTF_KEYUP, 0);
        End;

 //CapsLock handling:
 CapsLockState := (keys[VK_CAPITAL] > 0);
  If CapsLockState <> True Then    //Turn capslock on
    If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  //=== Win95/98
     begin
       keys[VK_CAPITAL] := 1;
       SetKeyboardState(keys);
     end
       Else
         If o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   //=== WinNT
          begin
            //Simulate Key Press
            keybd_event(VK_CAPITAL, $45, KEYEVENTF_EXTENDEDKEY Or 0, 0);
           //Simulate Key Release
           keybd_event (VK_CAPITAL, $45, KEYEVENTF_EXTENDEDKEY
             Or KEYEVENTF_KEYUP, 0);
          End;

 //ScrollLock handling:
 ScrollLockState := (keys[VK_SCROLL] > 0);
 If ScrollLockState <> True Then  //Turn Scroll lock on
  If o.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then  //=== Win95/98
   begin
     keys[VK_SCROLL] := 1;
     SetKeyboardState(keys);
   end
    Else
      If o.dwPlatformId = VER_PLATFORM_WIN32_NT Then   //=== WinNT
       begin
         //Simulate Key Press
         keybd_event(VK_SCROLL, $45, KEYEVENTF_EXTENDEDKEY Or 0, 0);
         //Simulate Key Release
         keybd_event(VK_SCROLL, $45, KEYEVENTF_EXTENDEDKEY
            Or KEYEVENTF_KEYUP, 0);
        End;
end;

end.

This tutorial was kindly provided by Serge Perevoznyk


Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips