-
November 30th, 2007, 11:14 AM
#1
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
-
November 30th, 2007, 12:28 PM
#2
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
-
December 1st, 2007, 01:28 AM
#3
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.)
-
August 7th, 2011, 11:31 AM
#4
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;
}
-
August 8th, 2011, 11:11 AM
#5
Re: Not catching VK_RETURN or VK_ESCAPE.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|