我的鼠标和键盘Hook.
你只需要在 KeyboardHookCallBack 判断keycode是不是WIN键,如果是就在
Result := CallNextHookEx(HookHandle, Code, KeyCode, Info);
这一行把keycode改成你要的键的键值.(理论上是,我没试过,我的hook程序是正确的)
library Hookdll2;
uses WinTypes, WinProcs, Messages;
type
PSharedData = ^TSharedData;
TSharedData = record
HookCount: integer;
MonitorWnd: HWND;
MouseHookHandle: HHook;
WM_MONITORMOUSEMOVE: UINT;
KeyBoardHookHandle: HHook;
WM_MONITORKEYBOARD: UINT;
end;
var
hMapObject: THandle;
SharedData: PSharedData;
const
MESSAGE_MONITOR_MOUSE_MOVE = 'CEMIHookDLLMonitorMouseMoveMessage';
MESSAGE_MONITOR_KEYBOARD = 'CEMIHookDLLMonitorKeyboardMessage';
function GetMonitorMouseMoveMsg: UINT; export;
begin
if SharedData = NIL then
Result := 0
else
Result := SharedData^.WM_MONITORMOUSEMOVE;
end;
function GetMonitorKeyboardMsg: UINT; export;
begin
if SharedData = NIL then
Result := 0
else
Result := SharedData^.WM_MONITORKEYBOARD;
end;
function MouseHookCallBack(Code: integer; Msg: WPARAM; MouseHook: LPARAM): LRESULT; stdcall;
var
HookHandle: HHOOK;
MonitorWnd: HWND;
WM_MONITORMOUSEMOVE: UINT;
MouseHookStruct: PMouseHookStruct absolute MouseHook;
begin
if SharedData <> NIL then
begin
MonitorWnd := SharedData^.MonitorWnd;
HookHandle := SharedData^.MouseHookHandle;
WM_MONITORMOUSEMOVE := SharedData^.WM_MONITORMOUSEMOVE;
end
else
begin
WM_MONITORMOUSEMOVE := 0;
MonitorWnd := 0;
HookHandle := 0;
end;
if (Code >= 0) and (MonitorWnd <> 0) then
begin
if (Msg = WM_MOUSEMOVE) and (MouseHookStruct <> NIL) then
PostMessage(MonitorWnd, WM_MONITORMOUSEMOVE, MouseHookStruct^.pt.x,
MouseHookStruct^.pt.y);
Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
end
else
Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
end;
function KeyboardHookCallBack(Code: integer; KeyCode: WPARAM; Info: LPARAM): LRESULT; stdcall;
var
HookHandle: HHOOK;
MonitorWnd: HWND;
WM_MONITORKEYBOARD: UINT;
begin
if SharedData <> NIL then
begin
MonitorWnd := SharedData^.MonitorWnd;
HookHandle := SharedData^.KeyboardHookHandle;
WM_MONITORKEYBOARD := SharedData^.WM_MONITORKEYBOARD;
end
else
begin
WM_MONITORKEYBOARD := 0;
MonitorWnd := 0;
HookHandle := 0;
end;
if (Code >= 0) and (MonitorWnd <> 0) then
begin
PostMessage(MonitorWnd, WM_MONITORKEYBOARD, KeyCode, Info);
Result := CallNextHookEx(HookHandle, Code, KeyCode, Info);
end
else
Result := CallNextHookEx(HookHandle, Code, KeyCode, Info);
end;
function InstallHook(SystemHook: boolean; TaskHandle: THandle;
AMonitorWnd: HWND) : boolean; export;
function GetModuleHandleFromInstance: THandle;
var
s: array[0..512] of char;
begin
GetModuleFileName(hInstance, s, sizeof(s)-1);
Result := GetModuleHandle(s);
end;
begin
Result := FALSE;
if SharedData = NIL then
exit
else
with SharedData^ do
begin
Result := TRUE;
if HookCount = 0 then
begin
MonitorWnd := AMonitorWnd;
if SystemHook then
begin
MouseHookHandle := SetWindowsHookEx(WH_MOUSE, MouseHookCallBack, HInstance, 0);
KeyboardHookHandle := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookCallBack, HInstance, 0);
end
else
begin
MouseHookHandle := SetWindowsHookEx(WH_MOUSE, MouseHookCallBack,
GetModuleHandleFromInstance, TaskHandle);
KeyboardHookHandle := SetWindowsHookEx(WH_KEYBOARD, KeyboardHookCallBack,
GetModuleHandleFromInstance, TaskHandle);
end;
if (MouseHookHandle <> 0) and (KeyboardHookHandle <> 0) then
inc(HookCount)
else
Result := FALSE;
end
else
inc(HookCount);
end;
end;
function RemoveHook: boolean; export;
var
MHook, KHook: boolean;
begin
Result := FALSE;
if SharedData = NIL then
exit
else
with SharedData^ do
begin
if HookCount < 1 then exit;
Result := TRUE;
dec(HookCount);
if HookCount = 0 then
begin
MHook := UnhookWindowsHookEx(MouseHookHandle);
KHook := UnhookWindowsHookEx(KeyboardHookHandle);
result := MHook and KHook;
end;
end;
end;
function IsHookSet: boolean; export;
begin
if SharedData = NIL then
Result := FALSE
else
with SharedData^ do
Result := (HookCount > 0) and (MouseHookHandle <> 0) and (KeyboardHookHandle <> 0);
end;
procedure AllocSharedData;
var
Init: boolean;
begin
if hMapObject = 0 then
begin
hMapObject := CreateFileMapping(
THandle($FFFFFFFF), NIL, PAGE_READWRITE, 0, SizeOf(TSharedData),
'DFSHookDLLSharedDataBlock');
Init := GetLastError <> ERROR_ALREADY_EXISTS;
if hMapObject = 0 then exit;
SharedData := MapViewOfFile( hMapObject, FILE_MAP_WRITE, 0, 0, 0);
if SharedData = NIL then exit;
if Init then
FillChar(SharedData^, SizeOf(TSharedData), 0);
SharedData^.WM_MONITORMOUSEMOVE := RegisterWindowMessage(MESSAGE_MONITOR_MOUSE_MOVE);
SharedData^.WM_MONITORKEYBOARD := RegisterWindowMessage(MESSAGE_MONITOR_KEYBOARD);
end;
end;
procedure FreeSharedData;
begin
if hMapObject <> 0 then
try
try
UnMapViewOfFile(SharedData);
CloseHandle(hMapObject);
except
end;
finally
SharedData := NIL;
hMapObject := 0;
end;
end;
procedure EntryPointProc(Reason: Integer);
begin
case Reason of
DLL_PROCESS_DETACH: FreeSharedData;
DLL_PROCESS_ATTACH: AllocSharedData;
DLL_THREAD_ATTACH: {} ;
DLL_THREAD_DETACH: {} ;
end;
end;
exports
GetMonitorMouseMoveMsg,
GetMonitorKeyboardMsg,
InstallHook,
RemoveHook,
IsHookSet,
KeyboardHookCallBack,
MouseHookCallBack;
begin
SharedData := NIL;
hMapObject := 0;
DllProc := @EntryPointProc;
EntryPointProc(DLL_PROCESS_ATTACH);
end.