FIX: Edit Control in Property Page Does Not Get Return Keys (125645)



The information in this article applies to:

  • The Microsoft Foundation Classes (MFC), when used with:
    • Microsoft Visual C++, 32-bit Editions 2.0

This article was previously published under Q125645

SYMPTOMS

A multiline edit control with the ES_WANTRETURN style set does not receive return key messages (WM_KEYDOWN with VK_RETURN) when placed in a CPropertyPage. If there is an IDOK button on the parent CPropertySheet, the dialog box will be dismissed when the ENTER key is pressed in the edit control.

CAUSE

The WM_KEYDOWN message is pre-translated in the PreTranslateMessage function of CPropertyPage to perform default pushbutton handling. A consequence of this is that the VK_RETURN key will never be sent to a child control in a CPropertyPage.

RESOLUTION

To work around this problem, you can override the PreTranslateMessage function in your CPropertyPage derived class. In this function, you can check whether the control is an edit control and determine whether it should receive the WM_KEYDOWN message when the ENTER key is pressed.

NOTE: This problem may occur with other controls as well. You can check whether they need the message by sending a WM_GETDLGCODE message to the control and looking for DLGC_WANTALLKEYS or DLGC_WANTMESSAGE. The PreTranslateMessage function, which would accomplish both of these tasks, is included here:
BOOL CMyPropertyPage::PreTranslateMessage(MSG *pMsg)
    {
    if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
        {
        static const TCHAR szEdit[] = _T("edit");
        TCHAR szCompare[sizeof(szEdit)+1];
        HWND hFocus = ::GetFocus();

        // Check for "edit" controls which want the RETURN key.
        // If it doesn't want the return key then let it be
        // translated by just calling
        // CPropertyPage::PreTranslateMessage
        ::GetClassName(hFocus, szCompare, sizeof(szCompare));
        if (lstrcmpi(szCompare, szEdit) == 0)
            {
            if(::GetWindowLong(hFocus, GWL_STYLE) & ES_WANTRETURN)
                return FALSE;    // Don't translate...just send on
                                 // to control
            else
                return CPropertyPage::PreTranslateMessage(pMsg);
            }

        // Check for other controls which want the return key
        if (::SendMessage(hFocus,WM_GETDLGCODE,0,0) &
           (DLGC_WANTALLKEYS | DLGC_WANTMESSAGE))
            return FALSE;        // Don't translate it
        };

    return CPropertyPage::PreTranslateMessage(pMsg);
    }
				

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in Visual C++ version 2.1 for Windows NT.

Modification Type:MajorLast Reviewed:10/24/2003
Keywords:kbbug kbfix KB125645