钩子函数在xp可以运行,win7不行,怎么改?
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HHOOK hkb = NULL;
LRESULT CALLBACK keyboardProc(int ncode, WPARAM wparam,
LPARAM lparam)
{
DWORD flag = ((DWORD)lparam&0x40000000);
if(flag &&(HC_ACTION == ncode))
{
char keyname[2];
ZeroMemory(keyname,2);
switch(wparam)
{
case VK_ESCAPE:
Form1->Button1->Name = "ESC";
break;
}
return CallNextHookEx(hkb, ncode, wparam, lparam);
}
}
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
hkb = SetWindowsHookEx(WH_KEYBOARD,
(HOOKPROC)keyboardProc, HInstance, 0);
}
//---------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
UnhookWindowsHookEx(hkb);
}
//---------------------------------------
小弟初学,什么都不懂,求大虾指教!
[最优解释]
使用WH_KEYBOARD_LL代替 WH_KEYBOARD
hkb = SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)keyboardProc, HInstance, 0); //win7 64位 ok
LRESULT CALLBACK keyboardProc(int ncode, WPARAM wparam,
LPARAM lparam)
{
//DWORD flag = ((DWORD)lparam&0x40000000);
DWORD flag = ((DWORD)lparam );
KBDLLHOOKSTRUCT *pkbhs;
if(flag &&(HC_ACTION == ncode))
{
//char keyname[2];
//ZeroMemory(keyname,2);
pkbhs = (KBDLLHOOKSTRUCT*)lparam;
if (pkbhs->vkCode==VK_ESCAPE ) Form1->Button1->Caption = "ESC";
return CallNextHookEx(hkb, ncode, wparam, lparam);
}
}
[其他解释]
谢谢,修改后可以用了,太感谢你了
[其他解释]
如何改?