Solved

Enter key doesn't work

Posted on 1998-08-14
10
241 Views
Last Modified: 2013-11-20
After adding a tab control to my dialog app, I find that if I tab to a button on one of the child windows, the Enter key doesn't send a command message to the main window.  The spacebar does.  If I press enter while the focus is on a control on the main window, the program dies.  Please help.
0
Comment
Question by:duncan2
  • 8
  • 2
10 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 100 total points
Comment Utility
Answer coming...
0
 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
The program dies because the enter key is interpreted as pressing the default button in the dialog box.  The spacebar is the key that sends commands to the currently selected item in the dialog box.
0
 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
There will always be a default button, even if the box that says "Default button" is unchecked.  The default button check box is useful for overriding the default.
0
 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
In fact, even if there are no buttons in the app, the enter key will still close it, just as if there were.
0
 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
To prevent the enter key from doing anything, delete the default OK button, and put in your own.

Then go into class wizard, and add a function to handle what happens when the button is pressed.  In the function put in whatever code you had in the okay button that was already there.

But don't call the CDialog::OnOK member function.

After this your code will look something like:
void CYourDlg::OnNewOk()
{ // Your code here.

  // CDialog::OnOK ();
}

Then set this new button as the default button.  The enter key will now push your new button, not terminate the app.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
Now, if there were never any buttons in your app to begin with, simply override the CDialog::OnOK member function.  This new function should do nothing, i.e. it should look like this:

void CYourDlg::OnOK()
{ // Nothing here.
}

When you press the enter key, it will do nothing.
0
 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
If you have any questions about how to implement any of the above, or any other questions about anything, please ask.

Thank you.
0
 

Author Comment

by:duncan2
Comment Utility
I don't have any buttons on the main dialog.  There used to be some buttons, so I guess that's why the enter key worked before.  It always activated either the default button or the button with the input focus.  Now it doesn't have a button, so it's closing the program.

If I override CDialog::OnOK, that may fix the problem of killing the program, but I don't think it will fix my other problem.  I have moved the buttons that were on the main dialog to the tab control's child windows.  When I use the Tab key to put the input focus on one of these buttons, pressing Enter should activate that button.  I have handlers for these buttons in the child dialog's class.  These handlers get called if I press the space bar, but not if I press Enter.  In a normal dialog (not a child), if the focus is on a button, pressing Enter will activate that button.  If you can solve this problem, It would be worth another 100 points to me.  Thanks for your help.
0
 

Author Comment

by:duncan2
Comment Utility
I found an answer to my problem.  It turns out that when Enter is pressed while a button on a child window has focus, a message is sent to the main window instead of the child window containing the button.  If the spacebar is pressed, a message is sent to the child window.  I had to leave the handlers in the child's class to handle the spacebar, and manually add ON_BN_CLICKED macros to the main window class message map with handlers there as well to take care of the Enter key.

Overriding the OnOK function like you suggested fixed the other problem.  Thanks a lot.
0
 
LVL 6

Expert Comment

by:thresher_shark
Comment Utility
Glad I could help :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now