[转]Why doesn't OnKeyDown catch key events in a dialog-based MFC project?
When a dialog has controls on it, the dialog itself never gets the focus. It's stolen by the child controls. When you press a button, a WM_KEYDOWN message is sent to the control with focus so your CgDlg::OnKeyDown is never called. Override the dialog's PreTranslateMessage function if you want dialog to handle the WM_KEYDOWN message:
BOOL CMyDlg::PreTranslateMessage(MSG* pMsg) { // TODO: 在此添加专用代码和/或调用基类 if (WM_KEYDOWN == pMsg->message) { switch (pMsg->wParam) { case VK_HOME: { ... } break; ... default: break; } } return CDialog::PreTranslateMessage(pMsg); }

浙公网安备 33010602011771号