看看我的程序为什么不对?(50分)

  • 主题发起人 主题发起人 rebeccarice
  • 开始时间 开始时间
R

rebeccarice

Unregistered / Unconfirmed
GUEST, unregistred user!
function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
var
aHandle:THandle;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
if ((lParam and _KeyPressMask) = 0) and (GetKeyState(vk_F11) < 0) then
begin
Result := 1;
aHandle := FindWindow('NotePad',nil);
if aHandle <> 0 then
begin
SetForeGroundWindow(aHandle);
aHandle := GetWindow(aHandle,GW_Child);
PostMessage(aHandle,WM_KEYDOWN,VK_Control,0);
PostMessage(aHandle,WM_KEYDOWN,Byte('v'),0);
PostMessage(aHandle,WM_KEYUP,Byte('v'),0);
PostMessage(aHandle,WM_KEYUP,VK_Control,0);
end;
end;
end;

程序如上,这是在hook 中对键盘的监控,如果按了‘F11’,就模拟‘Ctrl+V’这个组合键,
但是结果却不对。如果用SendKey('^V',False) 这个函数,他就会模拟按键很多遍。
请各位大侠帮忙!
 
postMessage发送并不可靠,用SendKey('^V',False),就会使得钩子函数再次被调用,
只要加一个标志就可以了:

var
First:Boolean;

function KeyboardHookHandler(iCode: Integer;
wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
_KeyPressMask = $80000000;
var
aHandle:THandle;
begin
Result := 0;
If iCode < 0 Then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
Exit;
end;
if not first then exit;
first:=False;
if ((lParam and _KeyPressMask) = 0) and (GetKeyState(vk_F11) < 0) then
begin
Result := 1;
aHandle := FindWindow('NotePad',nil);
if aHandle <> 0 then
begin
SetForeGroundWindow(aHandle);
aHandle := GetWindow(aHandle,GW_Child);
SendKey('^V',False),
end;
end;
First:=True;
end;
 
后退
顶部