home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d456 / KEYLED.ZIP / KeyLed.pas < prev    next >
Pascal/Delphi Source File  |  2002-07-29  |  3KB  |  114 lines

  1. //---------------------------------------------------------------------
  2. //
  3. // KeyLed component
  4. //
  5. // Copyright (c) 2001-2002 WINSOFT
  6. //
  7. //---------------------------------------------------------------------
  8.  
  9. unit KeyLed;
  10.  
  11. interface
  12.  
  13. uses Classes;
  14.  
  15. type
  16.   TKeyLed = class(TComponent)
  17.   private
  18.     function GetAbout: String;
  19.     procedure SetAbout(const Value: String);
  20.     function GetCapsLock: Boolean;
  21.     procedure SetCapsLock(Value: Boolean);
  22.     function GetNumLock: Boolean;
  23.     procedure SetNumLock(Value: Boolean);
  24.     function GetScrollLock: Boolean;
  25.     procedure SetScrollLock(Value: Boolean);
  26.   published
  27.     property About: String read GetAbout write SetAbout stored False;
  28.     property CapsLock: Boolean read GetCapsLock write SetCapsLock stored False;
  29.     property NumLock: Boolean read GetNumLock write SetNumLock stored False;
  30.     property ScrollLock: Boolean read GetScrollLock write SetScrollLock stored False;
  31.   end;
  32.  
  33. procedure Register;
  34.  
  35. implementation
  36.  
  37. uses Windows, SysUtils;
  38.  
  39. function GetKeyState(Key: Integer): Boolean;
  40. var Buffer: TKeyboardState;
  41. begin
  42.   GetKeyboardState(Buffer);
  43.   Result := Buffer[Key] <> 0;
  44. end;
  45.  
  46. procedure SetKeyState(Key: Integer; TurnOn: Boolean);
  47. var Buffer: TKeyboardState;
  48. begin
  49.   case Win32Platform of
  50.     VER_PLATFORM_WIN32_WINDOWS: // Win95/98/ME
  51.       begin
  52.         GetKeyboardState(Buffer);
  53.         Buffer[Key] := Ord(TurnOn);
  54.         SetKeyboardState(Buffer)
  55.       end;
  56.     VER_PLATFORM_WIN32_NT: // WinNT/2000/XP
  57.       begin
  58.         if GetKeyState(Key) <> TurnOn then
  59.         begin
  60.           keybd_event(Key, $45, KEYEVENTF_EXTENDEDKEY, 0); // simulate key press
  61.           keybd_event(Key, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0); // simulate key release
  62.         end;
  63.       end
  64.   end;
  65. end;
  66.  
  67. function TKeyLed.GetAbout: String;
  68. begin
  69.   Result := 'Version 1.1, Copyright (c) 2001-2002 WINSOFT, http://www.winsoft.sk';
  70. end;
  71.  
  72. procedure TKeyLed.SetAbout(const Value: String);
  73. begin
  74. end;
  75.  
  76. function TKeyLed.GetCapsLock: Boolean;
  77. begin
  78.   Result := GetKeyState(VK_CAPITAL)
  79. end;
  80.  
  81. procedure TKeyLed.SetCapsLock(Value: Boolean);
  82. begin
  83.   if not (csLoading in ComponentState) then
  84.     SetKeyState(VK_CAPITAL, Value);
  85. end;
  86.  
  87. function TKeyLed.GetNumLock: Boolean;
  88. begin
  89.   Result := GetKeyState(VK_NUMLOCK)
  90. end;
  91.  
  92. procedure TKeyLed.SetNumLock(Value: Boolean);
  93. begin
  94.   if not (csLoading in ComponentState) then
  95.     SetKeyState(VK_NUMLOCK, Value);
  96. end;
  97.  
  98. function TKeyLed.GetScrollLock: Boolean;
  99. begin
  100.   Result := GetKeyState(VK_SCROLL)
  101. end;
  102.  
  103. procedure TKeyLed.SetScrollLock(Value: Boolean);
  104. begin
  105.   if not (csLoading in ComponentState) then
  106.     SetKeyState(VK_SCROLL, Value);
  107. end;
  108.  
  109. procedure Register;
  110. begin
  111.   RegisterComponents('System', [TKeyLed]);
  112. end;
  113.  
  114. end.