CnGlobalKeyHook.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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$
  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 Integer(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.