/cnvcl/Source/NonVisual/CnGlobalKeyHook.pas

http://cnpack.googlecode.com/ · Pascal · 364 lines · 275 code · 49 blank · 40 comment · 24 complexity · 5861441433e4bdf51d5c3985c71ea5eb MD5 · raw file

  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { ???????????????? }
  4. { (C)Copyright 2001-2006 CnPack ??? }
  5. { ------------------------------------ }
  6. { }
  7. { ?????????????????? CnPack ??????? }
  8. { ??????????? }
  9. { }
  10. { ????????????????????????????? }
  11. { ?????????????????????? CnPack ????? }
  12. { }
  13. { ??????????????? CnPack ?????????? }
  14. { ????????????? }
  15. { }
  16. { ?????http://www.cnpack.org }
  17. { ?????master@cnpack.org }
  18. { }
  19. {******************************************************************************}
  20. unit CnGlobalKeyHook;
  21. {* |<PRE>
  22. ================================================================================
  23. * ????????????
  24. * ???????????????
  25. * ?????rarnu(rarnu@cnpack.org)
  26. * ? ??????API????dll????
  27. * ?????Windows2003 Server + Delphi2007 up2
  28. * ?????Windows2000/XP/2003/Vista + Delphi 7/2006/2007/2009
  29. * ? ? ????????????????????
  30. * ?????$Id: CnGlobalKeyHook.pas 138 2009-07-14 03:23:28Z zhoujingyu $
  31. * ?????2008.08.14 V1.0
  32. * ????
  33. ================================================================================
  34. |</PRE>}
  35. interface
  36. {$I CnPack.inc}
  37. uses
  38. SysUtils, Classes, Messages, Windows, Menus, Forms;
  39. type
  40. TCnHotKeyItem = class(TCollectionItem)
  41. private
  42. FHotKey : TShortCut;
  43. FOnExecute : TNotifyEvent;
  44. FApplicationToFront : Boolean;
  45. FID : Integer;
  46. procedure Changed;
  47. procedure SetHotKey(const Value: TShortCut);
  48. procedure SetOnExecute(const Value: TNotifyEvent);
  49. procedure SetApplicationToFront(const Value: Boolean);
  50. procedure DoExecute;
  51. protected
  52. function GetDisplayName: string; override;
  53. public
  54. constructor Create(Collection: TCollection); override;
  55. destructor Destroy; override;
  56. published
  57. {* ??? Application ???? }
  58. property ApplicationToFront: Boolean read FApplicationToFront
  59. write SetApplicationToFront default True;
  60. {* ?????? }
  61. property HotKey: TShortCut read FHotKey write SetHotKey default 0;
  62. {* ?????????? }
  63. property OnExecute: TNotifyEvent read FOnExecute write SetOnExecute;
  64. end;
  65. TCnHotKeyCollection = class(TOwnedCollection)
  66. private
  67. function GetItem(Index: Integer): TCnHotKeyItem;
  68. procedure SetItem(Index: Integer; const Value: TCnHotKeyItem);
  69. public
  70. constructor Create(AOwner: TPersistent);
  71. function Add: TCnHotKeyItem;
  72. function FindItemID(ID: Integer): TCnHotKeyItem;
  73. function Insert(Index: Integer): TCnHotKeyItem;
  74. {* ???? }
  75. property Items[Index: Integer]: TCnHotKeyItem read GetItem write SetItem;
  76. default;
  77. end;
  78. TCnCustomGlobalKeyHook = class(TComponent)
  79. private
  80. FHotKeys : TCnHotKeyCollection;
  81. FIDs : array of Integer;
  82. FHandle : THandle;
  83. FActive : Boolean;
  84. procedure SetHotKeys(const Value: TCnHotKeyCollection);
  85. procedure SetActive(const Value: Boolean);
  86. procedure WndProc(var Message: TMessage);
  87. protected
  88. procedure Changed;
  89. property HotKeys: TCnHotKeyCollection read FHotKeys write SetHotKeys;
  90. property Active: Boolean read FActive write SetActive;
  91. public
  92. constructor Create(AOwner: TComponent); override;
  93. destructor Destroy; override;
  94. end;
  95. TCnGlobalKeyHook = class(TCnCustomGlobalKeyHook)
  96. published
  97. {* ???? }
  98. property HotKeys;
  99. {* ?????????? }
  100. property Active;
  101. end;
  102. implementation
  103. type
  104. TIDManager = class
  105. private
  106. FIDs : array of Integer;
  107. public
  108. function GetAvailableID: Integer;
  109. procedure ReleaseID(const ID: Integer);
  110. end;
  111. var
  112. IDManager : TIDManager;
  113. function TIDManager.GetAvailableID: Integer;
  114. var
  115. Ok : Boolean;
  116. Index : Integer;
  117. begin
  118. Result := $F000;
  119. repeat
  120. Ok := True;
  121. for Index := Low(FIDs) to High(FIDs) do
  122. begin
  123. if FIDs[Index] = Result then
  124. begin
  125. Inc(Result);
  126. Ok := False;
  127. Break;
  128. end;
  129. end;
  130. until Ok;
  131. SetLength(FIDs, Length(FIDs)+1);
  132. FIDs[High(FIDs)] := Result;
  133. end;
  134. procedure TIDManager.ReleaseID(const ID: Integer);
  135. var
  136. Index : Integer;
  137. begin
  138. for Index := Low(FIDs) to High(FIDs) do
  139. begin
  140. if FIDs[Index] = ID then
  141. begin
  142. if Index < High(FIDs) then
  143. FIDs[Index] := FIDs[High(FIDs)];
  144. SetLength(FIDs, Length(FIDs)-1);
  145. Break;
  146. end;
  147. end;
  148. end;
  149. procedure TCnHotKeyItem.Changed;
  150. begin
  151. (TCnHotKeyCollection(Collection).GetOwner as TCnCustomGlobalKeyHook).Changed;
  152. end;
  153. constructor TCnHotKeyItem.Create(Collection: TCollection);
  154. begin
  155. inherited;
  156. FID := IDManager.GetAvailableID;
  157. FApplicationToFront := True;
  158. end;
  159. destructor TCnHotKeyItem.Destroy;
  160. begin
  161. IDManager.ReleaseID(FID);
  162. inherited;
  163. end;
  164. procedure TCnHotKeyItem.DoExecute;
  165. begin
  166. if FApplicationToFront then
  167. SetForegroundWindow(Application.Handle);
  168. if Assigned(FOnExecute) then
  169. FOnExecute(TCnHotKeyCollection(Collection).GetOwner);
  170. end;
  171. function TCnHotKeyItem.GetDisplayName: string;
  172. begin
  173. if FHotKey <> 0 then
  174. Result := ShortCutToText(FHotKey)
  175. else
  176. Result := inherited GetDisplayName;
  177. end;
  178. procedure TCnHotKeyItem.SetApplicationToFront(const Value: Boolean);
  179. begin
  180. if Value <> FApplicationToFront then
  181. begin
  182. FApplicationToFront := Value;
  183. Changed;
  184. end;
  185. end;
  186. procedure TCnHotKeyItem.SetHotKey(const Value: TShortCut);
  187. begin
  188. if Value <> FHotKey then
  189. begin
  190. FHotKey := Value;
  191. Changed;
  192. end;
  193. end;
  194. procedure TCnHotKeyItem.SetOnExecute(const Value: TNotifyEvent);
  195. begin
  196. FOnExecute := Value;
  197. Changed;
  198. end;
  199. procedure TCnCustomGlobalKeyHook.Changed;
  200. var
  201. Index : Integer;
  202. ShortCut : TShortCut;
  203. Modifiers : Cardinal;
  204. begin
  205. for Index := Low(FIDs) to High(FIDs) do
  206. UnregisterHotKey(FHandle, FIDs[Index]);
  207. SetLength(FIDs, 0);
  208. if FActive and (not (csDesigning in ComponentState)) then
  209. begin
  210. for Index := 0 to FHotKeys.Count-1 do
  211. begin
  212. if (FHotKeys[Index].HotKey <> 0) and
  213. (Assigned(FHotKeys[Index].OnExecute) or
  214. FHotKeys[Index].ApplicationToFront) then
  215. begin
  216. SetLength(FIDs, Length(FIDs)+1);
  217. FIDs[High(FIDs)] := FHotKeys[Index].FID;
  218. ShortCut := FHotKeys[Index].HotKey;
  219. Modifiers := 0;
  220. if (ShortCut and scShift) <> 0 then
  221. begin
  222. Modifiers := Modifiers or MOD_SHIFT;
  223. ShortCut := ShortCut and (not scShift);
  224. end;
  225. if (ShortCut and scCtrl) <> 0 then
  226. begin
  227. Modifiers := Modifiers or MOD_CONTROL;
  228. ShortCut := ShortCut and (not scCtrl);
  229. end;
  230. if (ShortCut and scAlt) <> 0 then
  231. begin
  232. Modifiers := Modifiers or MOD_ALT;
  233. ShortCut := ShortCut and (not scAlt);
  234. end;
  235. if not RegisterHotKey(FHandle, FIDs[High(FIDs)], Modifiers, ShortCut) then
  236. begin
  237. SetLength(FIDs, Length(FIDs)-1);
  238. RaiseLastWin32Error;
  239. end;
  240. end;
  241. end;
  242. end;
  243. end;
  244. constructor TCnCustomGlobalKeyHook.Create(AOwner: TComponent);
  245. begin
  246. inherited;
  247. if not (csDesigning in ComponentState) then
  248. FHandle := AllocateHWnd(WndProc);
  249. FActive := True;
  250. FHotKeys := TCnHotKeyCollection.Create(Self);
  251. end;
  252. destructor TCnCustomGlobalKeyHook.Destroy;
  253. begin
  254. Active := False;
  255. FHotKeys.Free;
  256. if FHandle <> 0 then
  257. DeallocateHWnd(FHandle);
  258. inherited;
  259. end;
  260. procedure TCnCustomGlobalKeyHook.SetActive(const Value: Boolean);
  261. begin
  262. if Value <> FActive then
  263. begin
  264. FActive := Value;
  265. Changed;
  266. end;
  267. end;
  268. procedure TCnCustomGlobalKeyHook.SetHotKeys(
  269. const Value: TCnHotKeyCollection);
  270. begin
  271. FHotKeys.Assign(Value);
  272. end;
  273. procedure TCnCustomGlobalKeyHook.WndProc(var Message: TMessage);
  274. var
  275. Index : Integer;
  276. begin
  277. if Message.Msg = WM_HOTKEY then
  278. begin
  279. for Index := 0 to FHotKeys.Count-1 do
  280. if Message.WParam = FHotKeys[Index].FID then
  281. FHotKeys[Index].DoExecute;
  282. end else
  283. Message.Result := DefWindowProc(FHandle, Message.Msg, Message.WParam,
  284. Message.LParam);
  285. end;
  286. function TCnHotKeyCollection.Add: TCnHotKeyItem;
  287. begin
  288. Result := inherited Add as TCnHotKeyItem;
  289. end;
  290. constructor TCnHotKeyCollection.Create(AOwner: TPersistent);
  291. begin
  292. inherited Create(AOwner, TCnHotKeyItem);
  293. end;
  294. function TCnHotKeyCollection.FindItemID(ID: Integer): TCnHotKeyItem;
  295. begin
  296. Result := inherited FindItemID(ID) as TCnHotKeyItem;
  297. end;
  298. function TCnHotKeyCollection.GetItem(Index: Integer): TCnHotKeyItem;
  299. begin
  300. Result := inherited Items[Index] as TCnHotKeyItem;
  301. end;
  302. function TCnHotKeyCollection.Insert(Index: Integer): TCnHotKeyItem;
  303. begin
  304. Result := inherited Insert(Index) as TCnHotKeyItem;
  305. end;
  306. procedure TCnHotKeyCollection.SetItem(Index: Integer;
  307. const Value: TCnHotKeyItem);
  308. begin
  309. inherited Items[Index] := Value;
  310. end;
  311. initialization
  312. IDManager := TIDManager.Create;
  313. finalization
  314. IDManager.Free;
  315. end.