|
我以前在PB5.0中建立了一个工程,做过这个键盘钩子程序。楼主可以参考下面的代码:
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
hInstanceload = hInstance;
if(!EnableKeyboardCapture())
return FALSE;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LoadVolumeChangeHook, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_LoadVolumeChangeHook);
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode < 0) // do not process message
return CallNextHookEx(hhkKeyboard, nCode, wParam, lParam);
if((nCode==HC_ACTION)&&(wParam==WM_KEYDOWN)){
PKBDLLHOOKSTRUCT p=(PKBDLLHOOKSTRUCT)lParam;
if ((p->vkCode == VK_RETURN))
{
{
printf("KeyboardProc::VK_RETURN\n");
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
}
return true;
}
}
return CallNextHookEx(hhkKeyboard, nCode, wParam, lParam);
}
BOOL EnableKeyboardCapture() {
if(!(hhkKeyboard=SetWindowsHookEx(WH_KEYBOARD_LL,(HOOKPROC)KeyboardProc,hInstanceload,0)))
{
printf("EnableKeyboardCapture failure\n");
return FALSE;
}
printf("EnableKeyboardCapture successfully\n");
return TRUE;
} |
|