下面的代码是鼠标键盘钩子的代码,没有问题,可以准确的捕获鼠标和键盘按键的状态!
但是必须要配合
SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
才可以;
我现在不想用钩子函数中的WH_JOURNALRECORD来设置钩子,
如果用
SetWindowsHookEx(WH_KEYBOARD, @HOOK_KEYBOARD, HInstance, 0)
SetWindowsHookEx(WH_MOUSE, @HOOK_MOUSE, HInstance, 0);
该如何得到具体按键和鼠标的按键状态呢,下面的代码肯定是不行了,请高手进来指点下,最好有完整详细的代码,谢谢了
function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;
begin
result := '[Print Screen]';
{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }
{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }
case lp of
14354: //alt 键,需要拦截 WM_SYSKEYDOWN
if wp > 30000 then
result := '[Right-Alt]'
else
result := '[Left-Alt]';
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '\';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '[LEFT]';
19751: result := '[RIGHT]';
7181: result := '[Enter]';
end;
end;//钩子回调过程
function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
var
s:string;
begin
if (PEventMsg(lparam)^.message = WM_SYSKEYDOWN) then
begin
s:=format('键盘按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
END; if (PEventMsg(lparam)^.message = WM_KEYDOWN) then
begin
//事件消息,键盘按下
s:=format('键盘按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (PEventMsg(lparam)^.message = WM_KEYUP) then
begin
//事件消息,键盘抬起
s:=format(' 键盘抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_LBUTTONDOWN) then
begin
s:=format('鼠标左键按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_LBUTTONUP) then
begin
s:=format(' 鼠标左键抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_RBUTTONDOWN) then
begin
s:=format('鼠标右键按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_RBUTTONUP) then
begin
s:=format(' 鼠标右键抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_MBUTTONDOWN) then
begin
s:=format('鼠标中键按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_MBUTTONUP) then
begin
s:=format(' 鼠标中键抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end;end;
但是必须要配合
SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
才可以;
我现在不想用钩子函数中的WH_JOURNALRECORD来设置钩子,
如果用
SetWindowsHookEx(WH_KEYBOARD, @HOOK_KEYBOARD, HInstance, 0)
SetWindowsHookEx(WH_MOUSE, @HOOK_MOUSE, HInstance, 0);
该如何得到具体按键和鼠标的按键状态呢,下面的代码肯定是不行了,请高手进来指点下,最好有完整详细的代码,谢谢了
function TForm1.Keyhookresult(lP: integer; wP: integer): pchar;
begin
result := '[Print Screen]';
{ VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) }
{ VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) }
case lp of
14354: //alt 键,需要拦截 WM_SYSKEYDOWN
if wp > 30000 then
result := '[Right-Alt]'
else
result := '[Left-Alt]';
10688: result := '`';
561: Result := '1';
818: result := '2';
1075: result := '3';
1332: result := '4';
1589: result := '5';
1846: result := '6';
2103: result := '7';
2360: result := '8';
2617: result := '9';
2864: result := '0';
3261: result := '-';
3515: result := '=';
4177: result := 'Q';
4439: result := 'W';
4677: result := 'E';
4946: result := 'R';
5204: result := 'T';
5465: result := 'Y';
5717: result := 'U';
5961: result := 'I';
6223: result := 'O';
6480: result := 'P';
6875: result := '[';
7133: result := ']';
11228: result := '\';
7745: result := 'A';
8019: result := 'S';
8260: result := 'D';
8518: result := 'F';
8775: result := 'G';
9032: result := 'H';
9290: result := 'J';
9547: result := 'K';
9804: result := 'L';
10170: result := ';';
10462: result := '''';
11354: result := 'Z';
11608: result := 'X';
11843: result := 'C';
12118: result := 'V';
12354: result := 'B';
12622: result := 'N';
12877: result := 'M';
13244: result := ',';
13502: result := '.';
13759: result := '/';
13840: result := '[Right-Shift]';
14624: result := '[Space]';
283: result := '[Esc]';
15216: result := '[F1]';
15473: result := '[F2]';
15730: result := '[F3]';
15987: result := '[F4]';
16244: result := '[F5]';
16501: result := '[F6]';
16758: result := '[F7]';
17015: result := '[F8]';
17272: result := '[F9]';
17529: result := '[F10]';
22394: result := '[F11]';
22651: result := '[F12]';
10768: Result := '[Left-Shift]';
14868: result := '[CapsLock]';
3592: result := '[Backspace]';
3849: result := '[Tab]';
7441:
if wp > 30000 then
result := '[Right-Ctrl]'
else
result := '[Left-Ctrl]';
13679: result := '[Num /]';
17808: result := '[NumLock]';
300: result := '[Print Screen]';
18065: result := '[Scroll Lock]';
17683: result := '[Pause]';
21088: result := '[Num0]';
21358: result := '[Num.]';
20321: result := '[Num1]';
20578: result := '[Num2]';
20835: result := '[Num3]';
19300: result := '[Num4]';
19557: result := '[Num5]';
19814: result := '[Num6]';
18279: result := '[Num7]';
18536: result := '[Num8]';
18793: result := '[Num9]';
19468: result := '[*5*]';
14186: result := '[Num *]';
19053: result := '[Num -]';
20075: result := '[Num +]';
21037: result := '[Insert]';
21294: result := '[Delete]';
18212: result := '[Home]';
20259: result := '[End]';
18721: result := '[PageUp]';
20770: result := '[PageDown]';
18470: result := '[UP]';
20520: result := '[DOWN]';
19237: result := '[LEFT]';
19751: result := '[RIGHT]';
7181: result := '[Enter]';
end;
end;//钩子回调过程
function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
var
s:string;
begin
if (PEventMsg(lparam)^.message = WM_SYSKEYDOWN) then
begin
s:=format('键盘按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
END; if (PEventMsg(lparam)^.message = WM_KEYDOWN) then
begin
//事件消息,键盘按下
s:=format('键盘按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(peventMsg(lparam)^.paramL, peventmsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (PEventMsg(lparam)^.message = WM_KEYUP) then
begin
//事件消息,键盘抬起
s:=format(' 键盘抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH])+Form1.Keyhookresult(PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_LBUTTONDOWN) then
begin
s:=format('鼠标左键按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_LBUTTONUP) then
begin
s:=format(' 鼠标左键抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_RBUTTONDOWN) then
begin
s:=format('鼠标右键按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_RBUTTONUP) then
begin
s:=format(' 鼠标右键抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_MBUTTONDOWN) then
begin
s:=format('鼠标中键按下:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end
else if (peventmsg(lparam)^.message=WM_MBUTTONUP) then
begin
s:=format(' 鼠标中键抬起:%5d %5d ',[PEventMsg(lparam)^.paramL,PEventMsg(lparam)^.paramH]);
Form1.ListBox1.Items.Add(s);
end;end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货