Link to home
Start Free TrialLog in
Avatar of cdgough
cdgough

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

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 cdgough
cdgough

ASKER

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.
>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.
Avatar of cdgough

ASKER

Thanks for the follow up comments, they were very helpful.