• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1096
  • Last Modified:

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.
0
cdgough
Asked:
cdgough
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now