1

I did a SetFocus to a button in a dialog. The button gets the dashed outline. When the user presses the return key, the dialog get a IDOK message rather than a message from the button were I set the focus. The same thing happens under other circumstances.

Why is this happening? And how can I cause the return to act as a button press?

Plain c++ windows app, no MFC, no NET.

Mike D
  • 2,753
  • 8
  • 44
  • 77
  • I don't know if in Windows API exists the Procedure like OnInitDialog;if there is, try to put that button as first in Tab Order and return TRUE on OnInitDialog(). I had precisely the opposite problem — Enter not doing the OnOK as a response to IDOK button — because return TRUE focuses the first button by Tab Order: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644995%28v=vs.85%29.aspx – sergiol Feb 02 '16 at 00:02
  • Continuation of my comment: See Question and Answer on http://stackoverflow.com/questions/35143211/enter-key-does-not-trigger-idok-default-push-button-action/35143302 – sergiol Feb 02 '16 at 01:19

3 Answers3

2

Feature, not a bug. The [Enter] key operates the button that's marked as the default button for a dialog. Either with the DEFPUSHBUTTON in the .rc file or the BS_DEFPUSHBUTTON style flag. Which is typically the "OK" button so getting IDOK back is expected. The [Escape] key is special that way too, typically the [Cancel] button. This is bound to ring a bell if you think back on how you used dialogs before.

You click a button that has the focus by pressing the space bar instead.

sergiol
  • 4,122
  • 4
  • 47
  • 81
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Ok (no pun intended) but the funny thing is this dialog has no button with IDC = IDOK. No extant button gets selected by the return so I presume that means no button has the DEFPUSHBUTTON style. I made one of the buttons default and that now gets selected by the return. – Mike D Apr 14 '13 at 17:12
1

In another SO question I found KB article that might help you:

If a dialog box or one of its controls currently has the input focus, then pressing the ENTER key causes Windows to send a WM_COMMAND message with the idItem (wParam) parameter set to the ID of the default command button. If the dialog box does not have a default command button, then the idItem parameter is set to IDOK by default.

When an application receives the WM_COMMAND message with idItem set to the ID of the default command button, the focus remains with the control that had the focus before the ENTER key was pressed. Calling GetFocus() at this point returns the handle of the control that had the focus before the ENTER key was pressed. The application can check this control handle and determine whether it belongs to any of the edit controls in the dialog box. If it does, then the user was entering data into one of the edit controls and after doing so, pressed ENTER. At this point, the application can send the WM_NEXTDLGCTL message to the dialog box to move the focus to the next control.

Community
  • 1
  • 1
Konrad Gadzina
  • 3,407
  • 1
  • 18
  • 29
0

According to MSDN,

Dialog Box Keyboard Interface

The system provides a special keyboard interface for dialog boxes that carries out special processing for several keys. The interface generates messages that correspond to certain buttons in the dialog box or changes the input focus from one control to another. Following are the keys used in this interface and their respective actions.

...

ENTER: Sends a WM_COMMAND message to the dialog box procedure. The wParam parameter is set to IDOK or control identifier of the default push button.

Since the system intercepts and processes ENTER key pressed directly through the dialog, you'll need to handle it in your dialog box procedure by calling GetFocus() to first see which control has the focus, and perform the appropriate action for that particular control.

JosephH
  • 8,465
  • 4
  • 34
  • 62