Link to home
Start Free TrialLog in
Avatar of GEN1040
GEN1040

asked on

Return key in CEdit on CDialogBar

How do I trap a return key in a CEdit derived class when the edit control is on a CDialogBar?  Preferably, I would like the solution to be CEdit related, i.e. CMyEdit::OnReturnKey()

Yes it does matter if the CEdit control is on a CDailogBar, that's the problem, somehow the CDialogBar trapps the return key and the WM_CHAR, WM_KEYUP, WM_KEYDOWN, etc. never come through so that the OnChar(), OnKeyUp(), or OnKeyDown() get called.

If your getting the OnChar() message in your CEdit derived class when it's on a CDialogBar let me know.  I certainly could be doing something else wrong.

ASKER CERTIFIED SOLUTION
Avatar of sapek
sapek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GEN1040
GEN1040

ASKER

Edited text of question
Well, I checked that and I know what is going on. CDialogBar is like a modeless dialog box and MFC calls IsDialogMessage for it and if it returns TRUE MFC never calls DispatchMessage for the message (this is what you are supposed to do for modeless dialogs). For WM_KEYDOWN(VK_RETURN) and WM_KEYDOWN(VK_ESCAPE) IsDialogMessage returns TRUE and produces WM_COMMAND(IDOK) and WM_COMMAND(IDCANCEL) which are sent to the dialog box.
This means that WM_KEYDOWN(VK_RETURN) will never be dispached to an edit control in a CDialogBar (or other modeless dialog) although such a message is in the message queue.
So how you can work around it? Assuming you don't have a IDOK button on the CDialogBar you can handle WM_COMMAND(IDOK) in a CDialogBar derived class and SendMessage WM_CHAR(VK_RETURN) to a control with focus. Then you can handle OnChar in your CEdit derived class.

Adam

Now I think I see the whole picture, now.
While investigating your question, the second thing I tried was subclassing the edit and it didn't work. So I was somewhat supriced that it worked for you. It turned out that I tired WM_KEYDOWN and you tried WM_KEYUP. While the later realy makes it to edit control the first one is swallowed by the IsDialogMessage and translated into the WM_COMMAND(IDOK). When I failed with WM_KEYDOWN I never thought about trying WM_KEYUP.

Anyway it turned out to be quite interesting problem.

Adam