在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 {-------------------------------------------------------------------------------
2 热键注册 3 作者: 张金利 日期: 2010.08.12 4 说明: 5 -------------------------------------------------------------------------------} 6 unit Utility.HotKeys; 7 8 interface 9 uses 10 Windows,Messages,Classes,SysUtils; 11 Type 12 {热键事件} 13 THotKeyEvent = procedure(const Shift,Key:Cardinal) of object; 14 {一个热键} 15 THotKey = record 16 Shift:Cardinal; 17 Key:Cardinal; 18 Event:THotKeyEvent; 19 end; 20 {已注册的热键} 21 TRegisteredHotKey = record 22 Atom:Word; 23 HotKey:THotKey; 24 Registered:Boolean; 25 end; 26 27 {热键} 28 THotKeys = class 29 private 30 {接收句柄} 31 FHotKeyHandle:THandle; 32 FAtom:string; 33 FHotKeys:array of TRegisteredHotKey; 34 {空热键} 35 function IsEmptyHotKey(const HotKey:THotKey):Boolean; 36 {取消注册} 37 procedure UnRegisterHotKeys; 38 {取得热键} 39 function GetHotKeys(const Index:Integer):THotKey; 40 {注册的数量} 41 function GetCount:Integer; 42 {两个热键相同} 43 function SameHotKey(const HotKeyA,HotKeyB:THotKey):Boolean; 44 {热键事件} 45 function GetHotKeyEvent(const HotKey:THotKey):THotKeyEvent; 46 {热键消息捕捉过程} 47 procedure HotKeyHooks(var Message: TMessage);message WM_HOTKEY; 48 public 49 constructor Create(const Atom:string); 50 destructor Destroy;override; 51 {添加热键} 52 procedure AddHotKey(const HotKey:THotKey); 53 {注册} 54 procedure RegisterHotKeys; 55 property HotKeys[const Index:Integer]:THotKey read GetHotKeys; 56 property Count:Integer read GetCount; 57 end; 58 59 function EmptyHotKey:THotKey; 60 61 implementation 62 63 function EmptyHotKey:THotKey; 64 begin 65 Result.Shift:=0; 66 Result.Key:=0; 67 Result.Event:=nil; 68 end; 69 70 { THotKeys } 71 72 procedure THotKeys.AddHotKey(const HotKey: THotKey); 73 begin 74 if IsEmptyHotKey(HotKey) then Exit; 75 SetLength(FHotKeys,Count + 1); 76 with FHotKeys[Count - 1].HotKey do 77 begin 78 Shift:=HotKey.Shift; 79 Key:=HotKey.Key; 80 Event:=HotKey.Event; 81 end; 82 FHotKeys[Count - 1].Registered:=False; 83 end; 84 85 constructor THotKeys.Create(const Atom:string); 86 begin 87 FHotKeyHandle:=AllocateHWnd(HotKeyHooks); 88 FAtom:=Atom; 89 end; 90 91 destructor THotKeys.Destroy; 92 begin 93 UnRegisterHotKeys; 94 DeallocateHWnd(FHotKeyHandle); 95 inherited; 96 end; 97 98 function THotKeys.GetCount: Integer; 99 begin 100 Result:=Length(FHotKeys); 101 end; 102 103 function THotKeys.GetHotKeys(const Index: Integer): THotKey; 104 begin 105 Result:=EmptyHotKey; 106 if Index in [0..Count] then 107 if FHotKeys[index].Registered then 108 Result:=FHotKeys[index].HotKey; 109 end; 110 111 function THotKeys.GetHotKeyEvent(const HotKey: THotKey): THotKeyEvent; 112 var 113 i:Integer; 114 begin 115 Result:=nil; 116 for i:=Low(FHotKeys) to High(FHotKeys) do 117 if SameHotKey(FHotKeys[i].HotKey,HotKey) then 118 begin 119 Result:=FHotKeys[i].HotKey.Event; 120 Break; 121 end; 122 end; 123 124 procedure THotKeys.HotKeyHooks(var Message: TMessage); 125 var 126 AHotKey:THotKey; 127 AHotKeyEvent:THotKeyEvent; 128 begin 129 AHotKey.Shift:=Message.LParamLo; 130 AHotKey.Key:=Message.LParamHi; 131 AHotKeyEvent:=GetHotKeyEvent(AHotKey); 132 if Assigned(AHotKeyEvent) then 133 AHotKeyEvent(AHotKey.Shift,AHotKey.Key); 134 end; 135 136 function THotKeys.IsEmptyHotKey(const HotKey: THotKey): Boolean; 137 begin 138 with HotKey do 139 Result:= (Shift = 0 ) or (Key = 0); 140 end; 141 142 procedure THotKeys.RegisterHotKeys; 143 var 144 Atom:Word; 145 HotKey:THotKey; 146 i:Integer; 147 begin 148 for i:= Low(FHotKeys) to High(FHotKeys) do 149 begin 150 HotKey:=FHotKeys[i].HotKey; 151 Atom:=GlobalAddAtom(PChar(FAtom + '_' + IntToStr(HotKey.Key))); 152 FHotKeys[i].Atom:=Atom; 153 if RegisterHotKey(FHotKeyHandle,Atom ,HotKey.Shift,HotKey.Key) then 154 FHotKeys[Count - 1].Registered:=True; 155 end; 156 end; 157 158 function THotKeys.SameHotKey(const HotKeyA, HotKeyB: THotKey): Boolean; 159 begin 160 Result:=(HotKeyA.Shift = HotKeyB.Shift) and (HotKeyA.Key = HotKeyB.Key); 161 end; 162 163 procedure THotKeys.UnRegisterHotKeys; 164 var 165 i:Integer; 166 begin 167 for i:= Low(FHotKeys) to High(FHotKeys) do 168 begin 169 if FHotKeys[i].Registered then 170 begin 171 UnRegisterHotKey(FHotKeyHandle,FHotKeys[i].Atom); 172 GlobalDeleteAtom(FHotKeys[i].Atom); 173 end; 174 end; 175 end; 176 177 end.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论