[转]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);
}

 

posted @ 2017-01-12 10:09  下雨的傍晚  阅读(131)  评论(0)    收藏  举报