Search code examples
c++eventsmfcmessage

MFC CFormView OnKeyDown event not triggered


I have a CFormView and I have some controls on it. I have implemented an OnKeyDown event on my CFormView. Everything is good, except my event is never triggered because the focus is on a combo box in my CFormView. I don't want the combo box to receive the event. I want the CFormView to receive it. So I implemented PreTranslateMessage(MSG* pMsg) and it removes the event from the combo box but it never gives it to the CFormView. Here is the code:

BOOL CfinalprojView::PreTranslateMessage(MSG* pMsg) { 
if( pMsg->message == WM_KEYDOWN ) {
    SendMessage( WM_COMMAND, MAKEWPARAM( IDD_FINALPROJ_FORM, BN_CLICKED ), ( LPARAM )0 );
    return TRUE; 
} else if( pMsg->message == WM_KEYUP ) {
    return TRUE; 
} else {
    return CFormView::PreTranslateMessage( pMsg );
}}

What am I doing wrong?

Thank you in advance,

Corneliu


Solution

  • So, it seems that there is no way to forward the key events to the CFormView. Instead, one can catch the events in PreTranslateMessage(MSG* pMsg) and check the key that was pressed like this:

    BOOL CfinalprojView::PreTranslateMessage(MSG* pMsg) {
        if( pMsg->message == WM_KEYDOWN ) {
            if( pMsg->wParam == VK_DELETE ) {
                ...
            }
            return TRUE; 
        }
        else if( pMsg->message == WM_KEYUP )
            return TRUE; 
        else
            return CFormView::PreTranslateMessage( pMsg );
    }