首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

键盘钩子的有关问题,好久了,还没有能解决,请各位大侠出手

2012-04-18 
键盘钩子的问题,好久了,还没有能解决,请各位大侠出手functionKeyboardHookHandler(iCode:IntegerwParam:W

键盘钩子的问题,好久了,还没有能解决,请各位大侠出手
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;
k:=1;
while   k <124   do
begin
      if   ((lParam   and   _KeyPressMask)   =   0)   and   (wParam   =   k   )   then
      begin
            SendMessage(HWND_BROADCAST,UnMsg[k],0,0);
      end;
      if   ((GetKeyState(VK_LCONTROL) <0)   and   (wParam   =   k))     then
      begin
            SendMessage(HWND_BROADCAST,UnMsg[k+1000],0,0);//这里的组合键消息每次都发一大堆,如何解决啊
      end;
      k:=k+1;
end;
end;

问题1:解决以上代码中的问题(以上是一个dll里的代码)
问题2:谁能给我一个dll代码,可以钩住键盘组合键的代码,谢谢了,我只能钩住单击,不能钩住组合键.

[解决办法]
bCtrlKeyDown := Boolean(GetAsyncKeyState(VK_CONTROL) shr ((sizeof(SHORT) * 8) - 1));

bWinKeyDown :=Boolean(GetAsyncKeyState(VK_LWIN) shr ((sizeof(SHORT) * 8) - 1)) or
Boolean(GetAsyncKeyState(VK_RWIN) shr ((sizeof(SHORT) * 8) - 1));

[解决办法]
建议你用记录钩子,那样会比较方便,也比较好用。。
[解决办法]
library KeyBoard;

uses
SysUtils,
Windows,
ShellApi,
Messages,
WinProcs,
Classes;

var
KBHook:HHook;
IsHooked:boolean;

function KeyBoardProc(Code:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
const
_keypressmask=$80000000;
begin
result:=0;
if Code <0 then
begin
CallNextHookEx(KBHook,Code,wParam,lParam);
end;

if ((lparam and _keypressmask)=0) and (GetAsyncKeyState(VK_CONTROL) <0) and (GetAsyncKeyState(VK_MENU) <0) and (wParam=47) then
begin
ShellExecute(0, 'Open ', 'NotePad.exe ',nil,nil,SW_SHOW);
result:=1;
end;

end;

function StartHook:boolean;stdcall;
begin
result:=false;
if IsHooked then exit;
KBHook:=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardProc,hInstance,0);
if KBHook <> 0 then
begin
Result:=true;
IsHooked:=true
end
else
IsHooked:=false;
end;

function RemoveHook:boolean;stdcall;
begin
result:=false;
if (not IsHooked) and (KBHook <> 0) then exit;
UnHookWindowsHookEx(KBHook);
Result:=true;
IsHooked:=false;
end;

exports
StartHook,RemoveHook;

{$R *.res}

begin
end.

热点排行