1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114:
| library hooklib;
uses Windows, Messages, SysUtils;
type PHookRec = ^THookRec; THookRec = record AppHnd: Integer; end;
const memFile: PAnsiChar = 'Global7v9k'; var Hooked: Boolean; hKeyHook, hMemFile, hApp: HWND; PHookRec1: PHookRec;
function KeyHookFunc(Code, VirtualKey, KeyStroke: Integer): LRESULT; stdcall; var KeyboardState: TKeyBoardState; begin Result := 0; if Code = HC_NOREMOVE then Exit;
GetKeyboardState(KeyboardState); if KeyboardState[VK_SCROLL]=1 then begin Result := CallNextHookEx(hKeyHook, Code, 48, KeyStroke); end else begin Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke); end;
if Code<0 then Exit;
if Code = HC_ACTION then begin if ((KeyStroke and (1 shl 30)) <> 0) then begin if not IsWindow(hApp) then begin hMemFile := OpenFileMapping(FILE_MAP_WRITE, False, memFile); PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0);
if PHookRec1<>nil then hApp := PHookRec1.AppHnd; end;
SendMessage(hApp, WM_USER + 1678, VirtualKey, KeyStroke); end; end; end; function StartHook(AppHandle: HWND): Byte; export; begin Result := 0;
if Hooked then begin Result := 1; Exit; end;
hKeyHook := SetWindowsHookEx(WH_KEYBOARD, KeyHookFunc, hInstance, 0); if hKeyHook>0 then begin hMemFile := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, SizeOf(THookRec), memFile); PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0); hApp := AppHandle; PHookRec1.AppHnd := AppHandle; Hooked := True; end else Result := 2; end;
function StopHook: Boolean; export; begin if PHookRec1<>nil then begin UnmapViewOfFile(PHookRec1); CloseHandle(hMemFile); PHookRec1 := nil; end;
if Hooked then Result := UnhookWindowsHookEx(hKeyHook) else Result := True; Hooked := False; end;
procedure EntryProc(dwReason: DWORD); begin if dwReason=Dll_Process_Detach then begin if PHookRec1 <> nil then begin UnmapViewOfFile(PHookRec1); CloseHandle(hMemFile); end; UnhookWindowsHookEx(hKeyHook); end; end;
exports StartHook, StopHook;
begin PHookRec1 := nil; Hooked := False; hKeyHook := 0; DLLProc := @EntryProc; EntryProc(Dll_Process_Attach); end. |