15,979
社区成员
发帖
与我相关
我的任务
分享
LRESULT CALLBACK KeyHookProc(int nCode, WPARAM wParam, LPARAM lParam)// 键盘钩子函数消息过程
{
if(nCode < 0 || nCode == HC_NOREMOVE)
return ::CallNextHookEx(g_hHook, nCode, wParam, lParam);
if(lParam & 0x40000000) // 消息重复就交给下一个hook链
{
return ::CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
我不太明白 if(nCode < 0 || nCode == HC_NOREMOVE)这个判断的作用
BOOL CHotKeysAppDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message==WM_KEYDOWN)
{
if(pMsg->wParam>=VK_F1 )
{
MessageBox(_T("F1"),_T("MessageBox"),MB_OK);
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
//比如我注册CTRL+D是显示窗口,CTRL+F是隐藏窗口
#define HOTKEY_HIDE 0xB0FF
#define HOTKEY_RESTORE 0xB100
#define VK_D 'D'
#define VK_F 'F'
//初始化时注册热键
BOOL result=RegisterHotKey(this->GetSafeHwnd(),HOTKEY_HIDE,MOD_CONTROL,VK_D);
result=RegisterHotKey(this->GetSafeHwnd(),HOTKEY_RESTORE,MOD_CONTROL,VK_F);
//添加WM_HOTKEY消息响应热键
afx_msg void OnHotKey(WPARAM wParam,LPARAM lParam);
ON_MESSAGE(WM_HOTKEY,OnHotKey)
void CXXXDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
{
if(HOTKEY_HIDE==(int) wParam)
ShowWindow(SW_HIDE);
if(HOTKEY_RESTORE==(int) wParam)
ShowWindow(SW_SHOW);
}
//卸载热键
::UnregisterHotKey(this->GetSafeHwnd(),HOTKEY_HIDE);
::UnregisterHotKey(this->GetSafeHwnd(),HOTKEY_RESTORE);