BS_DEFPUSHBUTTON child of non-dialog window: Doesn't receive Enter?

I have a DEFPUSHBUTTON that I have added to my main window using CreateWindowEx:


1
2
3
buttonWnd = CreateWindowEx( WS_EX_WINDOWEDGE, L"BUTTON", L"Send Input",
		WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 0,0,0,0,	// Sizing left to WM_SIZE
		hWnd, (HMENU)IDC_BUTTONPRIME, hInst, NULL);



I want it to respond as if clicked when the user presses the Enter key. Specifically, that should happen if the focus is:
1) In the parent window itself,
2) In a (single-line) EDIT control that is also a child of the parent window, or
3) On the button itself.

What is needed to make that happen? I've been having a terrible time trying to get this to work. Presently, even if the button itself has the focus, pressing Enter still won't trigger it (Spacebar will, though). Do I need to manually send BN_CLICKED messages in response to each of those three cases, or is there a cleaner solution, since it's (supposedly) the default pushbutton?

If I do need to handle every focus case manually, what's the correct way to catch the Enter keypress when an EDIT control has focus? I've spent several hours trying to look up a way to do this, but the closest solutions I've seen seem very hack-ish.

(If it's relevant, here's my message loop:)

1
2
3
4
5
6
7
8
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
	if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg) )
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}
Last edited on
Topic archived. No new replies allowed.