Home > other >  How to create an enter key triggered Edit control which clicks a button on pressing enter in win32 a
How to create an enter key triggered Edit control which clicks a button on pressing enter in win32 a

Time:03-18

I want to create an edit control that behaves in the following manner: While user is entering text in the edit control, when enter is pressed by the user, it clicks a button. I searched a lot but found nothing that helps. How to do it in C Win32 API.

CodePudding user response:

The short answer is: "don't do that."

Create a dialog containing the edit control and button. Make sure the edit control's ES_WANTRETURN style is turned off, and the button's BS_DEFPUSHBUTTON style is turned on.

ES_WANTRETURN controls whether the edit control processes a carriage return/enter key (VK_RETURN keystroke). With it turned on, the edit control processes the keystroke itself (it becomes a new-line in the edit control). Probably obvious, but ES_WANTRETURN is generally only useful for a multi-line edit control.

With ES_WANTRETURN turned off, the enter/return keystroke is passed to the parent (the dialog box). That will process an enter as pressing the default button (if there is one).

CodePudding user response:

Simply give the Button control the BS_DEFPUSHBUTTON style, then it will react to Enter presses automatically.

Otherwise, you would have to subclass the Edit control to catch WM_KEY(DOWN|UP) messages sent to it (don't catch them in the parent window's WndProc), check for VK_RETURN, and if detected then send a BM_CLICK message to the Button control.

  • Related