Focus button not responding to Enter key.

I have a custom button class that is derived from the MFC CButton class. This class is an owner-drawn button who's functionality is very similar to the CBitmapButton class, but with some special enhancements.

The button paints correctly, responds to mouse clicks correctly, and I can 'Tab' to it corrrectly. For some reason, it will not respond to the 'Enter' key when it is the focused button in the tab order. When the custom button is focused and the 'Enter' key is pressed, the dialog closes and DoModal() returns. Strange.

I'm thinking that it has something to do with being owner-drawn because if you throw a standard MFC button on a dialog it responds to 'Enter' just fine.

What am I missing?

I've already thought about capturing the 'Enter' key and sending WM_LBUTTONDOWN message to my button if it is focused, but that leaves a real bad taste in my mouth  -- it sounds like a sloppy work-around as opposed to the correct solution. Can somone please post the correct solution.

Thanks.
cdgoughAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
Even if there are no OK and Cancel buttons on the dialog box, the Enter key will still invoke OnOK or OnCancel. Try overriding these two functions. You may assign either IDOK or IDCANCEL to the button or use CDialog::SetDefID.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cdgoughAuthor Commented:
If I use a standard CButton, and press enter when it is focused on the dialog, the FOCUS button is pressed correctly.

If I use my custom button derived from CButton, and press enter when it is focused, the result is that the DEFAULT button is presed. If there is no default button, the IDOK() function is called (like you mentioned).

I'm interested in learning what I'm leaving out of my button class so it doesn't respond like a regular CButton. Can you tell me this?

Also, if you don't know -- I'll fix the problem doing the following. In the OnOk function, I'll determine the button that has the focus and send a WM_LBUTTONDOWN message to that button. Can you tell me how to determine the control that currently has the focus?

Thanks.
0
chensuCommented:
>I'm interested in learning what I'm leaving out of my button class so it doesn't respond like a regular CButton. Can you tell me this?

You may need to override the OnGetDlgCode of your button class to return the correct values (DLGC_DEFPUSHBUTTON) to the dialog box.

>Can you tell me how to determine the control that currently has the focus?

Call CWnd::GetFocus. You may send a BM_CLICK message instead of a WM_LBUTTONDOWN message to the button.
0
cdgoughAuthor Commented:
Thanks for the follow up comments, they were very helpful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.