⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpkeymaphook.pas

📁 键盘钩子程序及控件, C++ Builder 和DELPHI可用
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  VK_PACKET: VKeyName := 'VK_PACKET';
  VK_ATTN: VKeyName := 'VK_ATTN';
  VK_CRSEL: VKeyName := 'VK_CRSEL';
  VK_EXSEL: VKeyName := 'VK_EXSEL';
  VK_EREOF: VKeyName := 'VK_EREOF';
  VK_PLAY: VKeyName := 'VK_PLAY';
  VK_ZOOM: VKeyName := 'VK_ZOOM';
  VK_NONAME: VKeyName := 'VK_NONAME';
  VK_PA1: VKeyName := 'VK_PA1';
  VK_OEM_CLEAR: VKeyName := 'VK_OEM_CLEAR';
  else begin
       ScanCode := MapVirtualKeyEx(KeyHookStruct.vkCode, 0, GetKeyboardLayout(0)) shl 16;
       if FKeyState.ExtendedKey then ScanCode := ScanCode or $01000000;
       GetKeyNameText(ScanCode ,@VKeyName,SizeOf(VKeyName));
       end;
  end;

  move(VKeyName,FKeyNames.KeyExtName,SizeOf(VKeyName));
  GetKeyboardState(KBS);
  //if ToAscii(KeyHookStruct.vkCode, KeyHookStruct.scanCode, KBS, @VKeyChar, 0) <> 0
  if ToAsciiEx(KeyHookStruct.vkCode, KeyHookStruct.scanCode, KBS, @VKeyChar, ord(FKeyState.MenuKey), GetKeyboardLayout(0)) <> 0
  then FKeyNames.KeyChar := VKeyChar[1];

  if Assigned(FOnKey) then FOnKey(self,FKeyState,FKeyNames,WasBlocked);
  //msg.Result := 0;
end;

function TCPKeyMapHook.Start_KeyHook: Boolean;
begin
Result := False;
if FEnabled then exit;
if Not LoadHookLib then exit;
if PFncHookStart(FLicenceCode,FWindowHandle,FUserHookMsg, FDisableKeyboard, KeyMap) then begin
   FEnabled := True;
   Result := True;
   end;
end;

function TCPKeyMapHook.Stop_KeyHook: Boolean;
begin
Result := False;
Try
if FEnabled then Result := PFncHookStop;
FEnabled := False;
Finally
UnloadHookLib;
End;
end;

function TCPKeyMapHook.UpdateHook: boolean;
begin
Result := False;
if FEnabled then Result := PFncHookUpdateHook(FDisableKeyboard,KeyMap);
end;

procedure TCPKeyMapHook.ResetKeyMap;
begin
  KeyMap.VK_BLOCKKEY := VK_BLOCKKEY;
  KeyMap.VK_LBUTTON := VK_LBUTTON;
  KeyMap.VK_RBUTTON := VK_RBUTTON;
  KeyMap.VK_CANCEL := VK_CANCEL;
  KeyMap.VK_MBUTTON := VK_MBUTTON;
  KeyMap.VK_XBUTTON1 := VK_XBUTTON1;
  KeyMap.VK_XBUTTON2 := VK_XBUTTON2;
  KeyMap.VK_BACK := VK_BACK;
  KeyMap.VK_TAB := VK_TAB;
  KeyMap.VK_CLEAR := VK_CLEAR;
  KeyMap.VK_RETURN := VK_RETURN;
  KeyMap.VK_SHIFT := VK_SHIFT;
  KeyMap.VK_CONTROL := VK_CONTROL;
  KeyMap.VK_MENU := VK_MENU;
  KeyMap.VK_PAUSE := VK_PAUSE;
  KeyMap.VK_CAPITAL := VK_CAPITAL;
  KeyMap.VK_KANA := VK_KANA;
  KeyMap.VK_HANGUEL := VK_HANGUEL;
  KeyMap.VK_HANGUL := VK_HANGUL;
  KeyMap.VK_JUNJA := VK_JUNJA;
  KeyMap.VK_FINAL := VK_FINAL;
  KeyMap.VK_HANJA := VK_HANJA;
  KeyMap.VK_KANJI := VK_KANJI;
  KeyMap.VK_ESCAPE := VK_ESCAPE;
  KeyMap.VK_CONVERT := VK_CONVERT;
  KeyMap.VK_NONCONVERT := VK_NONCONVERT;
  KeyMap.VK_ACCEPT := VK_ACCEPT;
  KeyMap.VK_MODECHANGE := VK_MODECHANGE;
  KeyMap.VK_SPACE := VK_SPACE;
  KeyMap.VK_PRIOR := VK_PRIOR;
  KeyMap.VK_NEXT := VK_NEXT;
  KeyMap.VK_END := VK_END;
  KeyMap.VK_HOME := VK_HOME;
  KeyMap.VK_LEFT := VK_LEFT;
  KeyMap.VK_UP := VK_UP;
  KeyMap.VK_RIGHT := VK_RIGHT;
  KeyMap.VK_DOWN := VK_DOWN;
  KeyMap.VK_SELECT := VK_SELECT;
  KeyMap.VK_PRINT := VK_PRINT;
  KeyMap.VK_EXECUTE := VK_EXECUTE;
  KeyMap.VK_SNAPSHOT := VK_SNAPSHOT;
  KeyMap.VK_INSERT := VK_INSERT;
  KeyMap.VK_DELETE := VK_DELETE;
  KeyMap.VK_HELP := VK_HELP;
  KeyMap.VK_0 := VK_0;
  KeyMap.VK_1 := VK_1;
  KeyMap.VK_2 := VK_2;
  KeyMap.VK_3 := VK_3;
  KeyMap.VK_4 := VK_4;
  KeyMap.VK_5 := VK_5;
  KeyMap.VK_6 := VK_6;
  KeyMap.VK_7 := VK_7;
  KeyMap.VK_8 := VK_8;
  KeyMap.VK_9 := VK_9;
  KeyMap.VK_A := VK_A;
  KeyMap.VK_B := VK_B;
  KeyMap.VK_C := VK_C;
  KeyMap.VK_D := VK_D;
  KeyMap.VK_E := VK_E;
  KeyMap.VK_F := VK_F;
  KeyMap.VK_G := VK_G;
  KeyMap.VK_H := VK_H;
  KeyMap.VK_I := VK_I;
  KeyMap.VK_J := VK_J;
  KeyMap.VK_K := VK_K;
  KeyMap.VK_L := VK_L;
  KeyMap.VK_M := VK_M;
  KeyMap.VK_N := VK_N;
  KeyMap.VK_O := VK_O;
  KeyMap.VK_P := VK_P;
  KeyMap.VK_Q := VK_Q;
  KeyMap.VK_R := VK_R;
  KeyMap.VK_S := VK_S;
  KeyMap.VK_T := VK_T;
  KeyMap.VK_U := VK_U;
  KeyMap.VK_V := VK_V;
  KeyMap.VK_W := VK_W;
  KeyMap.VK_X := VK_X;
  KeyMap.VK_Y := VK_Y;
  KeyMap.VK_Z := VK_Z;
  KeyMap.VK_LWIN := VK_LWIN;
  KeyMap.VK_RWIN := VK_RWIN;
  KeyMap.VK_APPS := VK_APPS;
  KeyMap.VK_SLEEP := VK_SLEEP;
  KeyMap.VK_NUMPAD0 := VK_NUMPAD0;
  KeyMap.VK_NUMPAD1 := VK_NUMPAD1;
  KeyMap.VK_NUMPAD2 := VK_NUMPAD2;
  KeyMap.VK_NUMPAD3 := VK_NUMPAD3;
  KeyMap.VK_NUMPAD4 := VK_NUMPAD4;
  KeyMap.VK_NUMPAD5 := VK_NUMPAD5;
  KeyMap.VK_NUMPAD6 := VK_NUMPAD6;
  KeyMap.VK_NUMPAD7 := VK_NUMPAD7;
  KeyMap.VK_NUMPAD8 := VK_NUMPAD8;
  KeyMap.VK_NUMPAD9 := VK_NUMPAD9;
  KeyMap.VK_MULTIPLY := VK_MULTIPLY;
  KeyMap.VK_ADD := VK_ADD;
  KeyMap.VK_SEPARATOR := VK_SEPARATOR;
  KeyMap.VK_SUBTRACT := VK_SUBTRACT;
  KeyMap.VK_DECIMAL := VK_DECIMAL;
  KeyMap.VK_DIVIDE := VK_DIVIDE;
  KeyMap.VK_F1 := VK_F1;
  KeyMap.VK_F2 := VK_F2;
  KeyMap.VK_F3 := VK_F3;
  KeyMap.VK_F4 := VK_F4;
  KeyMap.VK_F5 := VK_F5;
  KeyMap.VK_F6 := VK_F6;
  KeyMap.VK_F7 := VK_F7;
  KeyMap.VK_F8 := VK_F8;
  KeyMap.VK_F9 := VK_F9;
  KeyMap.VK_F10 := VK_F10;
  KeyMap.VK_F11 := VK_F11;
  KeyMap.VK_F12 := VK_F12;
  KeyMap.VK_F13 := VK_F13;
  KeyMap.VK_F14 := VK_F14;
  KeyMap.VK_F15 := VK_F15;
  KeyMap.VK_F16 := VK_F16;
  KeyMap.VK_F17 := VK_F17;
  KeyMap.VK_F18 := VK_F18;
  KeyMap.VK_F19 := VK_F19;
  KeyMap.VK_F20 := VK_F20;
  KeyMap.VK_F21 := VK_F21;
  KeyMap.VK_F22 := VK_F22;
  KeyMap.VK_F23 := VK_F23;
  KeyMap.VK_F24 := VK_F24;
  KeyMap.VK_NUMLOCK := VK_NUMLOCK;
  KeyMap.VK_SCROLL :=VK_SCROLL ;
  KeyMap.VK_LSHIFT := VK_LSHIFT;
  KeyMap.VK_RSHIFT := VK_RSHIFT;
  KeyMap.VK_LCONTROL := VK_LCONTROL;
  KeyMap.VK_RCONTROL := VK_RCONTROL;
  KeyMap.VK_LMENU := VK_LMENU;
  KeyMap.VK_RMENU := VK_RMENU;
  KeyMap.VK_BROWSER_BACK := VK_BROWSER_BACK;
  KeyMap.VK_BROWSER_FORWARD := VK_BROWSER_FORWARD;
  KeyMap.VK_BROWSER_REFRESH := VK_BROWSER_REFRESH;
  KeyMap.VK_BROWSER_STOP := VK_BROWSER_STOP;
  KeyMap.VK_BROWSER_SEARCH := VK_BROWSER_SEARCH;
  KeyMap.VK_BROWSER_FAVORITES := VK_BROWSER_FAVORITES;
  KeyMap.VK_BROWSER_HOME := VK_BROWSER_HOME;
  KeyMap.VK_VOLUME_MUTE := VK_VOLUME_MUTE;
  KeyMap.VK_VOLUME_DOWN := VK_VOLUME_DOWN;
  KeyMap.VK_VOLUME_UP := VK_VOLUME_UP;
  KeyMap.VK_MEDIA_NEXT_TRACK := VK_MEDIA_NEXT_TRACK;
  KeyMap.VK_MEDIA_PREV_TRACK := VK_MEDIA_PREV_TRACK;
  KeyMap.VK_MEDIA_STOP := VK_MEDIA_STOP;
  KeyMap.VK_MEDIA_PLAY_PAUSE := VK_MEDIA_PLAY_PAUSE;
  KeyMap.VK_LAUNCH_MAIL := VK_LAUNCH_MAIL;
  KeyMap.VK_LAUNCH_MEDIA_SELECT := VK_LAUNCH_MEDIA_SELECT;
  KeyMap.VK_LAUNCH_APP1 := VK_LAUNCH_APP1;
  KeyMap.VK_LAUNCH_APP2 := VK_LAUNCH_APP2;
  KeyMap.VK_OEM_1 := VK_OEM_1;
  KeyMap.VK_OEM_PLUS := VK_OEM_PLUS;
  KeyMap.VK_OEM_COMMA := VK_OEM_COMMA;
  KeyMap.VK_OEM_MINUS := VK_OEM_MINUS;
  KeyMap.VK_OEM_PERIOD := VK_OEM_PERIOD;
  KeyMap.VK_OEM_2 := VK_OEM_2;
  KeyMap.VK_OEM_3 := VK_OEM_3;
  KeyMap.VK_OEM_4 := VK_OEM_4;
  KeyMap.VK_OEM_5 := VK_OEM_5;
  KeyMap.VK_OEM_6 :=VK_OEM_6 ;
  KeyMap.VK_OEM_7 := VK_OEM_7;
  KeyMap.VK_OEM_8 := VK_OEM_8;
  KeyMap.VK_OEM_102 := VK_OEM_102;
  KeyMap.VK_PROCESSKEY := VK_PROCESSKEY;
  KeyMap.VK_PACKET := VK_PACKET;
  KeyMap.VK_ATTN := VK_ATTN;
  KeyMap.VK_CRSEL := VK_CRSEL;
  KeyMap.VK_EXSEL := VK_EXSEL;
  KeyMap.VK_EREOF := VK_EREOF;
  KeyMap.VK_PLAY := VK_PLAY;
  KeyMap.VK_ZOOM := VK_ZOOM;
  KeyMap.VK_NONAME := VK_NONAME;
  KeyMap.VK_PA1 := VK_PA1;
  KeyMap.VK_OEM_CLEAR := VK_OEM_CLEAR;

  KeyMap.HK_CTRL_ESC := HK_CTRL_ESC;
  KeyMap.HK_ALT_ESC := HK_ALT_ESC;
  KeyMap.HK_ALT_TAB := HK_ALT_TAB;
  KeyMap.HK_ALT_F4 := HK_ALT_F4;
  KeyMap.HK_CTRL_BREAK := HK_CTRL_BREAK;
  KeyMap.HK_ALT_RETURN := HK_ALT_RETURN;
end;

function TCPKeyMapHook.LoadHookLib: boolean;
begin
result := false;
if FHookLibLoaded then exit;
DllHandle := LoadLibrary(PChar(HOOKLIBNAME));
if DllHandle <> 0 then
   begin
   { Get pointers to DLL Hook Functions }
   PFncHookStart := GetProcAddress(DllHandle, 'SystemKeyboardHook_Start');
   PFncHookStop := GetProcAddress(DllHandle, 'SystemKeyboardHook_Stop');
   PFncHookGetData := GetProcAddress(DllHandle, 'SystemKeyboardHook_GetData');
   PFncHookUpdateHook := GetProcAddress(DllHandle, 'SystemKeyboardHook_UpdateHook');
   if Assigned(PFncHookStart) and Assigned(PFncHookStop) and Assigned(PFncHookGetData) and Assigned(PFncHookUpdateHook) then
      begin
      FHookLibLoaded := True;
      result := true;
      end else FreeLibrary(DllHandle);
   end;
end;

function TCPKeyMapHook.UnloadHookLib: boolean;
begin
result := false;
if DllHandle <> 0 then
   begin
   FreeLibrary(DllHandle);
   FHookLibLoaded := false;
   result := true;
   end;
End;

initialization

finalization
 if DllHandle <> 0 then FreeLibrary(DllHandle);


end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -