展开全部
全局键盘钩子是需要放到dll中去实现的,日志钩子可以做到在一个程序中实现全局键盘捕获:
VC代码:
#include<windows.h>
HHOOK LogHook=NULL;
LRESULT CALLBACK LogProc(int code,WPARAM wParam,LPARAM lParam)
{
EVENTMSG *pEventMsg=(EVENTMSG*)lParam;
if(code==HC_ACTION)
{
switch(pEventMsg->message)
{
case (WM_KEYDOWN):
BYTE bKeyState[256];
unsigned short uAscii[4];
UINT uScanCode;
int iRet;
GetKeyboardState(bKeyState);
bKeyState[VK_SHIFT]=GetKeyState(VK_SHIFT);
uScanCode=(pEventMsg->paramH>>16);
iRet=ToAscii(pEventMsg->paramL,uScanCode,bKeyState,uAscii,0);
switch(iRet)
{
case 0:
break;
case 1:
if((char)uAscii[0]=='t' || (char)uAscii[0]=='T')//t键按下
{
MessageBox(NULL,"按下了T","按键提示",MB_OK);
/**************自己实现播放音乐的功能*****************/
}
else if((char)uAscii[0]=='y' || (char)uAscii[0]=='Y')//Y键按下
{
MessageBox(NULL,"按下了Y","按键提示",MB_OK);
/***************自己在这里实现停止播放音乐的功能****************/
}
break;
default:
break;
}
default:
break;
}
}
return CallNextHookEx(LogHook,code,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
LogHook=SetWindowsHookEx(WH_JOURNALRECORD,LogProc,hInstance,NULL);
while(1)Sleep(100);
return 0;
}
VC代码:
#include<windows.h>
HHOOK LogHook=NULL;
LRESULT CALLBACK LogProc(int code,WPARAM wParam,LPARAM lParam)
{
EVENTMSG *pEventMsg=(EVENTMSG*)lParam;
if(code==HC_ACTION)
{
switch(pEventMsg->message)
{
case (WM_KEYDOWN):
BYTE bKeyState[256];
unsigned short uAscii[4];
UINT uScanCode;
int iRet;
GetKeyboardState(bKeyState);
bKeyState[VK_SHIFT]=GetKeyState(VK_SHIFT);
uScanCode=(pEventMsg->paramH>>16);
iRet=ToAscii(pEventMsg->paramL,uScanCode,bKeyState,uAscii,0);
switch(iRet)
{
case 0:
break;
case 1:
if((char)uAscii[0]=='t' || (char)uAscii[0]=='T')//t键按下
{
MessageBox(NULL,"按下了T","按键提示",MB_OK);
/**************自己实现播放音乐的功能*****************/
}
else if((char)uAscii[0]=='y' || (char)uAscii[0]=='Y')//Y键按下
{
MessageBox(NULL,"按下了Y","按键提示",MB_OK);
/***************自己在这里实现停止播放音乐的功能****************/
}
break;
default:
break;
}
default:
break;
}
}
return CallNextHookEx(LogHook,code,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
LogHook=SetWindowsHookEx(WH_JOURNALRECORD,LogProc,hInstance,NULL);
while(1)Sleep(100);
return 0;
}
2009-04-15
展开全部
你是调用SetWindowsHookEx挂钩的吧
HHOOK SetWindowsHookEx( int idHook,
HOOKPROC lpfn,
HINSTANCE hMod,
DWORD dwThreadId
);
最后一个参数是指定要挂钩的线程ID,这个值为0的话就是全局钩了
HHOOK SetWindowsHookEx( int idHook,
HOOKPROC lpfn,
HINSTANCE hMod,
DWORD dwThreadId
);
最后一个参数是指定要挂钩的线程ID,这个值为0的话就是全局钩了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询