MFC OnKeyDown没反应,不响应键盘操作
清泛原创
在MFC中添加键盘的消息响应,通常是通过OnKeyDown函数来完成消息的捕捉和响应。
解决方案(重载PreTranslateMessage函数):
深入探索该问题请移步:
http://wenku.baidu.com/link?url=2eG1TLx_ARJHKO6LFtZU_u6AOv6Pw8RyemsJEXvaKYwvQRHAh43D-TnvQZvt3-U5JMZjRqE835qeArbKKscaO-YAxfwBkepkYbdbzc2iEZK
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
...
BEGIN_MESSAGE_MAP(CxxxDlg, CDialog)
......
ON_WM_KEYDOWN()
END_MESSAGE_MAP()
添加了消息映射后, 发现无论按下键盘的哪个键OnKeyDown函数体都没有被执行。基于对话框的程序是无法直接响应键盘消息的,需要重载虚函数PreTranslateMessage()。解决方案(重载PreTranslateMessage函数):
virtual BOOL PreTranslateMessage(MSG* pMsg);
...
BOOL CxxxDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_CONTROL)
{
AfxMessageBox(_T("Ctrl pressed!"));
}
}
return __super::PreTranslateMessage(pMsg);
}
深入探索该问题请移步:
http://wenku.baidu.com/link?url=2eG1TLx_ARJHKO6LFtZU_u6AOv6Pw8RyemsJEXvaKYwvQRHAh43D-TnvQZvt3-U5JMZjRqE835qeArbKKscaO-YAxfwBkepkYbdbzc2iEZK