学习VC++深入浅出——钩子的使用
HHOOK g_hMouse
=
NULL;
HHOOK g_hKeyboard = NULL;
HWND g_hWnd = NULL;
LRESULT CALLBACK MouseProc( int nCode,WPARAM wParam,LPARAM lParam)
... {
return 1;
}
LRESULT CALLBACK KeyboardProc( int nCode,WPARAM wParam,LPARAM lParam)
... {
//if(VK_SPACE == wParam)
/**//* if(VK_F4 == wParam && (1==(lParam>>29 & 1)))
return 1;
else
return CallNextHookEx(g_hKeyboard,nCode,wParam,lParam);*/
if(VK_F2 == wParam)
...{
::SendMessage(g_hWnd,WM_CLOSE,0,0);
UnhookWindowsHookEx(g_hKeyboard);
UnhookWindowsHookEx(g_hMouse);
}
return 1;
}
BOOL CInnerHookDlg::OnInitDialog()
... {
// TODO: Add extra initialization here
g_hWnd = m_hWnd;
g_hMouse = SetWindowsHookEx(WH_MOUSE,MouseProc,NULL,GetCurrentThreadId());
g_hKeyboard =SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,GetCurrentThreadId());
return TRUE; // return TRUE unless you set the focus to a control
}
HHOOK g_hKeyboard = NULL;
HWND g_hWnd = NULL;
LRESULT CALLBACK MouseProc( int nCode,WPARAM wParam,LPARAM lParam)
... {
return 1;
}
LRESULT CALLBACK KeyboardProc( int nCode,WPARAM wParam,LPARAM lParam)
... {
//if(VK_SPACE == wParam)
/**//* if(VK_F4 == wParam && (1==(lParam>>29 & 1)))
return 1;
else
return CallNextHookEx(g_hKeyboard,nCode,wParam,lParam);*/
if(VK_F2 == wParam)
...{
::SendMessage(g_hWnd,WM_CLOSE,0,0);
UnhookWindowsHookEx(g_hKeyboard);
UnhookWindowsHookEx(g_hMouse);
}
return 1;
}
BOOL CInnerHookDlg::OnInitDialog()
... {
// TODO: Add extra initialization here
g_hWnd = m_hWnd;
g_hMouse = SetWindowsHookEx(WH_MOUSE,MouseProc,NULL,GetCurrentThreadId());
g_hKeyboard =SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,GetCurrentThreadId());
return TRUE; // return TRUE unless you set the focus to a control
}