CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2001
    Posts
    2,455

    Not catching VK_RETURN or VK_ESCAPE.

    I have a CEdit derived class and I am trying to catch the VK_RETURN and VK_ESCAPE in the ON_WM_KEYDOWN message handler. It does not seem to get called when pressing escape key or enter key. Any other key it gets called.

    Any ideas?
    Code:
    BEGIN_MESSAGE_MAP(ElcEdit, CEdit)
    	//{{AFX_MSG_MAP(ElcEdit)
        ON_WM_KEYDOWN()
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    void ElcEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    	switch(nChar)
    	{
    	case VK_ESCAPE:
    		GetParent()->SendMessage(MsgEditCancelled, 0, 0);
    		break;
    	}
    }
    I places a breakpoint at the switch, but it doesn't break there if escape or enter is pressed. It seems to break for all other keys though.

    Mike B

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Not catching VK_RETURN or VK_ESCAPE.

    Try to handle the WM_GETDLGCODE message in your ElcEdit class and return DLGC_WANTALLKEYS.
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2007
    Posts
    613

    Re: Not catching VK_RETURN or VK_ESCAPE.

    The problem is caused by the fact that the edit control was designed to be used in a dialog box. In a dialog box pressing Enter causes the dialog box to be dismissed with IDOK. Pressing Esc causes the dialog box to be dismissed with IDCANCEL. Because any of these keys closes the dialog box the authors found pointless to send any message.

    You can force the Enter to be sent to you by specifiing the ES_WANTRETURN style for your edit control. This style will yield results only if you also specify the ES_MULTILINE style.

    (Explanation: normally, in a multiline edit box, you can go to the begining of a new line using Ctrl+Enter. If you use ES_WANTRETURN, you will have that functionality just pressing Enter. In this case, as long as the focus is on that edit control, Enter will not close the dialog box.)

  4. #4
    Join Date
    Aug 2011
    Posts
    1

    Resolved Re: Not catching VK_RETURN or VK_ESCAPE.

    Thanks! I had the same problem and the solution of VictorN worked for me:

    I simply added:

    MESSAGE_HANDLER(WM_GETDLGCODE, OnGetDlgCode)

    and

    LRESULT OnGetDlgCode( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
    {
    return DLGC_WANTALLKEYS;
    }

  5. #5
    Join Date
    Aug 2011
    Posts
    3

    Re: Not catching VK_RETURN or VK_ESCAPE.

    thanks ,


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured