We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Trapping <return> in dialog

porear asked
Medium Priority
Last Modified: 2010-04-10
I have no OK button or items in my dialog which call the OnOK() function.  However, any time return is hit my program terminates.

I have tried PreTranslateMessage to no avail (perhaps I am calling it in the wrong place).  How can I trap the return?

I also have a single line CEdit control, will I need to trap <Return> entered in it separately?  Thanks!

P.S. the daily points haven't been updating, sorry I can't spend much more on this!
Watch Question

Specify the ES_WANTRETURN style for the edit control.  

Let me know if you have questions.


From what I understand ES_WANTRETURN only applies to multiline edit boxes... this is a single line.  The main problem is the main dialog window, not just the edit box.  Thanks

I'm not sure this is a good way, but one way is to handle the WM_GETDLGCODE message and return DLGC_WANTALLKEYS.  This prevents the dialog window from interpreting the enter key.  However it prevents it form interpreting other keys as well.  Thus you would have to renable those behaviours that you wanted by looking for their keys and mimicing these standard behavior.   There may be an easier way.  I don't use the built-in dialogs, so I'm not sure.


I did try that too, but apparently there is still something wrong with my method.  Per something I found on a newsgroup:

In MyDlg.CPP:


              ON_WM_GETDLGCODE()  // I added these two to the existing list


UINT CMyDlg::OnGetDlgCode()

void CMyDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
              if (nChar == VK_RETURN)
                         MessageBox("Return hit");     //  Trapped the return, notify

Still no luck...  Thanks for the input though, I know here has to be a simple solution to this, its got to be a common issue...

This is just a shot in the dark, but I understood that Enter selects the 'default button', defaultness being a button property you can set using Developer Studio in the normal way.  Why don't you try sticking a dummy button with this default property, perhaps with a dummy do-nothing OnClick() handler.  If that solves your problem, keep the button and use MoveWindow() to shift the button out of the visible dialog box area inside OnInitDialog().

Good Luck,

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


Thanks, that solved the problem well (and easily I might add, I knew it could be done simply).  It does trap ALL returns now though, so I have to handle the ones I want myself, which is a little effort but worth it for my app.  

Warmcat's idea worked well too, I did not know about the default button properties (forgive my relative newness to MFC).  I'll post a separate message to give him/her points as well (I have some to offer now that they've finally updated my account). Thanks!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.