library Hooks;
uses
Windows,
Messages;
const
mousmsg='
MouseHookMessage_Assarbad';
keybmsg='
KeyboardHookMessage_Assarbad';
var
Mouse_HookHandle: Cardinal = 0;
Keyboard_HookHandle: Cardinal = 0;
WindowHandle: Cardinal = 0;
WM_MOUSEHOOKMSG: Cardinal = 0;
WM_KEYBHOOKMSG: Cardinal = 0;
function KeyboardHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
stdcall;
begin
//es ist ebenfalls moeglich die Bearbeitung an eine Bedingung zu knuepfen
//it's possible to call CallNextHookEx conditional only.
Result := CallNextHookEx(Keyboard_HookHandle, nCode, wParam, lParam);
case nCode < 0
of
TRUE: exit;
//wenn code kleiner 0 wird nix gemacht
//if code smaller 0 nothing has to be done
FALSE:
begin
//Hier kann jetzt alles bearbeitet werden
//Here one can work with the parameters
setprop(WindowHandle, '
keyb_ncode', nCode);
SendMessage(HWND_BROADCAST, WM_KEYBHOOKMSG, wParam, lParam);
end;
end;
end;
function MouseHookProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
stdcall;
begin
//es ist ebenfalls moeglich die Bearbeitung an eine Bedingung zu knuepfen
//it's possible to call CallNextHookEx conditional only.
Result := CallNextHookEx(Mouse_HookHandle, nCode, wParam, lParam);
case nCode < 0
of
TRUE: exit;
//wenn code kleiner 0 wird nix gemacht
//if code smaller 0 nothing has to be done
FALSE:
begin
//Hier kann jetzt alles bearbeitet werden
//Here one can work with the parameters
setprop(WindowHandle, '
mous_ncode', nCode);
setprop(WindowHandle, '
mous_hwnd', PMOUSEHOOKSTRUCT(lParam)^.hwnd);
setprop(WindowHandle, '
mous_hitt', PMOUSEHOOKSTRUCT(lParam)^.wHitTestCode);
setprop(WindowHandle, '
mous_xpos', PMOUSEHOOKSTRUCT(lParam)^.pt.x);
setprop(WindowHandle, '
mous_ypos', PMOUSEHOOKSTRUCT(lParam)^.pt.y);
SendMessage(HWND_BROADCAST, WM_MOUSEHOOKMSG, wParam, lParam);
end;
end;
end;
function InstallHooks(Hwnd: Cardinal): Boolean;
stdcall;
var mouseh, keybh: boolean;
begin
keybh := false;
mouseh := false;
//Erstmal Hooks installieren
//First install the hooks
case Mouse_HookHandle
of
0:
begin
Mouse_HookHandle := SetWindowsHookEx(WH_MOUSE, @MouseHookProc, HInstance, 0);
SetProp(Hwnd,mousmsg,WM_MOUSEHOOKMSG);
mouseh := true;
end;
end;
case Keyboard_HookHandle
of
0:
begin
Keyboard_HookHandle := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHookProc, HInstance, 0);
SetProp(Hwnd,keybmsg,WM_KEYBHOOKMSG);
keybh := true;
end;
end;
//Uebergebenes Fensterhandle sichern
//Save the given window handle
WindowHandle := Hwnd;
Result := keybh
and mouseh;
end;
function UninstallHooks: Boolean;
stdcall;
var mouseh, keybh: boolean;
begin
//Hooks aus der Hookchain entfernen
//Uninstall hook from hooks chain
mouseh := UnhookWindowsHookEx(Mouse_HookHandle);
keybh := UnhookWindowsHookEx(Keyboard_HookHandle);
Mouse_HookHandle := 0;
Keyboard_HookHandle := 0;
Result := keybh
and mouseh;
if Result
then WindowHandle := 0;
end;
exports
//Installations- und Deinstallationsroutine exportieren
//Export the installation and deinstallation routine
InstallHooks,
UninstallHooks;
begin
WM_MOUSEHOOKMSG:=RegisterWindowMessage(mousmsg);
WM_KEYBHOOKMSG:=RegisterWindowMessage(keybmsg);
end.