HKProc.pas
上传用户:mjqmds
上传日期:2022-05-05
资源大小:2827k
文件大小:3k
源码类别:

DirextX编程

开发平台:

Delphi

  1. unit HKProc;
  2. interface
  3. uses
  4.   Windows, Messages,SysUtils,SyncObjs,Classes,Variants, Registry;
  5. Const
  6.     WM_MYBACK=WM_USER+102;
  7.     WM_SHOWAPP=WM_USER+1108;
  8.     WM_HIDEAPP=WM_USER+1109;
  9.     WM_KILLAPP=WM_USER+1110;
  10.     
  11. function MessageHookProc(iCode: Integer;
  12.   wParam: WPARAM;
  13.   lParam: LPARAM): LRESULT; stdcall; export;
  14. function EnableHotKeyHook: BOOL; export;
  15. function DisableHotKeyHook: BOOL; export;
  16. procedure HotKeyHookExit; far;
  17. var
  18.   hNextHookProc: HHook;
  19.   procSaveExit: Pointer;
  20.   vHandle:THandle;
  21.   CriticalSetion:TCriticalSection;
  22. implementation
  23. uses Information;
  24. function ReadReg(Key: String; DefaultValue: Variant): Variant;
  25. Var
  26.     Reg:TRegistry;
  27. begin
  28.     Result:=NULL;
  29.     Reg:=TRegistry.Create;
  30.     Try
  31.         Reg.RootKey:=HKEY_CURRENT_USER;
  32.         if Reg.OpenKey('/WHSUPPERPLAYER/RUN',false) then
  33.         begin
  34.             Result:=Reg.ReadString(Key);
  35.             if VarIsNull(Result) or VarIsEmpty(Result) then
  36.                 Result:=DefaultValue;
  37.         end
  38.         else
  39.             Result:=DefaultValue;
  40.     Finally
  41.         Reg.Free;
  42.     end;
  43. end;
  44. function MessageHookProc(iCode: Integer;
  45.   wParam: WPARAM;
  46.   lParam: LPARAM): LRESULT; stdcall; export;
  47. const
  48.   _KeyPressMask = $80000000;
  49. Var
  50.   v:Variant;
  51. begin
  52.     Result := 0;
  53.     If iCode < 0 Then
  54.     begin
  55.         Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
  56.         Exit;
  57.     end;
  58.     // 侦测 Ctrl + B 组合键
  59.     if ((lParam and _KeyPressMask) = 0) and (GetKeyState(vk_Control) < 0) then
  60.     begin
  61.         v:=ReadReg('MsgHandle',null);
  62.         if VarIsNull(v) then
  63.             Exit;
  64.         Result := 1;
  65.         if wParam = Ord('R') then
  66.         begin
  67.             begin
  68.                 PostMessage(v,WM_SHOWAPP,0,0);
  69.             end;
  70.         end
  71.         else if wParam=Ord('Q') then
  72.         begin
  73.             PostMessage(v,WM_KILLAPP,0,0);
  74.         end
  75.     end
  76. end;
  77. function EnableHotKeyHook: BOOL; export;
  78. begin
  79.   Result := False;
  80.   if hNextHookProc <> 0 then Exit;
  81.   hNextHookProc := SetWindowsHookEx(WH_KEYBOARD,
  82.     MessageHookProc,
  83.     HInstance,
  84.     0);
  85.   Result := hNextHookProc <> 0;
  86. end;
  87. function DisableHotKeyHook: BOOL; export;
  88. begin
  89.   if hNextHookProc <> 0 then
  90.   begin
  91.     UnhookWindowshookEx(hNextHookProc);
  92.     hNextHookProc := 0;
  93.   end;
  94.   Result := hNextHookProc = 0;
  95. end;
  96. procedure HotKeyHookExit;
  97. begin
  98.   if hNextHookProc <> 0 then DisableHotKeyHook;
  99.   ExitProc := procSaveExit;
  100. end;
  101. begin
  102.   CriticalSetion:=TCriticalSection.Create;
  103. end.