home *** CD-ROM | disk | FTP | other *** search
/ Softwarová Záchrana 3 / Softwarova-zachrana-3.bin / ArsClip / source.zip / UnitHotKey.pas < prev    next >
Pascal/Delphi Source File  |  2003-07-30  |  3KB  |  122 lines

  1. unit UnitHotKey;
  2. {
  3.     Purpose:
  4.         Move all the hotkey logic into a contained object
  5.         (In other words, undo the adhoc bad design of the past)
  6.  
  7. }
  8. {///////////////}
  9. {//}interface{//}
  10. {///////////////}
  11. Type
  12. TACHotKey = class (TObject)
  13.     private
  14.         FormHandle : THandle;
  15.         HotKeyID : integer;
  16.         hkCtrl, hkAlt, hkShift, hkWinkwy : boolean;
  17.         hkKey : word;
  18.         function HotKeyAssign(
  19.             Ctrl: boolean; Alt: boolean; Shift: boolean; Winkey : boolean;
  20.             Keystroke: word ): boolean;
  21.     public
  22.         constructor Create(FormHandle : THandle; UniqueName : string;
  23.             Hotkey_Ctrl, Hotkey_Alt, Hotkey_Shift, Hotkey_Winkey : boolean;
  24.             Hotkey_key : word);
  25.         function SetHotKey(
  26.                 Hotkey_Ctrl, Hotkey_Alt, Hotkey_Shift, Hotkey_Winkey : boolean;
  27.                 Hotkey_key : word
  28.             ) : boolean;
  29.         function GetID : word;
  30.         destructor Destroy; override;
  31. end;
  32. {////////////////////}
  33. {//}implementation{//}
  34. {////////////////////}
  35. uses Windows, SysUtils, Forms, UnitFrmMainPopup;
  36.  
  37. constructor TACHotKey.Create(FormHandle : THandle; UniqueName : string;
  38.     Hotkey_Ctrl, Hotkey_Alt, Hotkey_Shift, Hotkey_Winkey : boolean;
  39.     Hotkey_key : word
  40.     );
  41. begin
  42.     Windows.SetLastError(ERROR_SUCCESS);
  43.  
  44.     self.FormHandle := formhandle;
  45.     HotKeyID := GlobalAddAtom(PChar(UniqueName));
  46.     if not self.HotKeyAssign(Hotkey_Ctrl, Hotkey_Alt, Hotkey_Shift,
  47.         Hotkey_Winkey, Hotkey_Key) then begin
  48.  
  49.         Raise Exception.Create('ERROR: Unable to assign a hotkey. Please change the key.');
  50.     end;
  51. end;
  52.  
  53. destructor TACHotKey.Destroy;
  54. begin
  55.     if (HotKeyID <> 0) then begin
  56.         UnRegisterHotKey(FormHandle, HotKeyID);
  57.         DeleteAtom(HotKeyID);
  58.     end;
  59. end;
  60.  
  61. function TACHotKey.GetID : word;
  62. begin
  63.     result := HotKeyID;
  64. end;
  65.  
  66. function TACHotKey.SetHotKey(
  67.     Hotkey_Ctrl, Hotkey_Alt, Hotkey_Shift, Hotkey_Winkey : boolean;
  68.     Hotkey_key : word
  69. ) : boolean;
  70. begin
  71.     Windows.SetLastError(ERROR_SUCCESS);
  72.     //
  73.     // if the user tries to assign to a hotkey that is already in use,
  74.     // the hotkeyrelease failing is OK
  75.     //
  76.     if HotKeyID <> 0 then begin
  77.         UnRegisterHotKey(FormHandle, HotKeyID);
  78.     end;
  79.  
  80.  
  81.     if not (self.HotKeyAssign(Hotkey_Ctrl, Hotkey_Alt, Hotkey_Shift, Hotkey_Winkey,
  82.         Hotkey_key)) then begin
  83.         Raise Exception.Create('ERROR: Couldn''t assign hotkey. It may already be assigned by another program.');
  84.     end else begin
  85.         result := true;
  86.     end;
  87. end;
  88.  
  89. //------------------------
  90. // Private Implementation
  91. //------------------------
  92. function TACHotKey.HotKeyAssign(
  93.     Ctrl: boolean; Alt: boolean; Shift: boolean; Winkey : boolean;
  94.     Keystroke: word ): boolean;
  95. var modifier: word;
  96. begin
  97.     modifier := 0;
  98.     if ctrl then modifier := modifier or MOD_CONTROL;
  99.     if alt then  modifier := modifier or MOD_ALT;
  100.     if shift then modifier := modifier or MOD_SHIFT;
  101.     if winkey then modifier := modifier or MOD_WIN;
  102.     result := false;
  103.     if (HotKeyID = 0) then begin
  104.         exit;
  105.     end;
  106.  
  107.     hkCtrl := ctrl;
  108.     hkAlt := alt;
  109.     hkShift := shift;
  110.     hkWinkwy := winkey;
  111.     hkKey := Keystroke;
  112.  
  113.     if not (Windows.RegisterHotKey(FormHandle, HotKeyID, modifier, Keystroke)) then begin
  114.         exit;
  115.     end;
  116.  
  117.     result := true;
  118. end;
  119.  
  120.  
  121. end.
  122.