press ENTER at EditBox to execute

Hi Guru,

Assuming I have an edit box and "send" button. After typing something in the edit box, I can send it either pressing ENTER or click SEND button.

How can I implement the "Pressing ENTER" action, without clicking SEND button ?
Meaning that Pressing Enter after finishing typing in edit box will have same action as clicking the "SEND" button. Clicking SEND button works fine for me.
[411 byte] By [Dimension] at [2007-11-18 19:17:16]
# 1 Re: press ENTER at EditBox to execute
For Send to fucntionality to be executed when you hit enter, the Send button should be the default button.

A default button is one which has a thick border ( visually ). Normally the OK button is for a dialog. You can change this either by
1. Resource. Open the dialog in resoure editor. Right click on Send button to invoke it's properties. IN the styles category , you will see a default button. Check it. and remove the same property from all buttons in the dialog.

2. Programmatically. Check help on DM_SETDEFID
kirants at 2007-11-9 13:09:59 >
# 2 Re: press ENTER at EditBox to execute
I guess I can't use this first approach, which is change the property setting. Because my "SEND" button is "OWNER DRAW" & BITMAPS, which I have a graphic SEND button. If i choose DEFAULT BUTTON option, it will uncheck OWNER DRAW option.
Dimension at 2007-11-9 13:10:57 >
# 3 Re: press ENTER at EditBox to execute
yeah. MSDN says that BS_OWNERDRAW should not be combined with any other button styles.

Here's what you do in WM_INITDIALOG handler for your dialog:

// Reset the current default push button to a regular button.
SendDlgItemMessage(hDlg, <ID of current default push button>,
BM_SETSTYLE, BS_PUSHBUTTON, (LONG)TRUE);

// Update the default push button's control ID.
SendMessage(hDlg, DM_SETDEFID, <ID of new default push button>,
0L);

// Set the new style.
SendDlgItemMessage(hDlg, <ID of new default push button>,
BM_SETSTYLE, BS_DEFPUSHBUTTON, (LONG)TRUE);
kirants at 2007-11-9 13:12:06 >
# 4 Re: press ENTER at EditBox to execute
Hi Dimension :)

When the user has finished typing in your edit box, the only way for enter to be detected is to sub class the control. Enter could be detected on the main DlgProc but when the edit box has focus, the detection could not happen. Sub class the edit control and check if the user presses the enter key with this:

case WM_KEYDOWN:
{
if(wParam == VK_ENTER)
{
// enter was pressed - do something here
}
}

Hope that helps :)
tomcant at 2007-11-9 13:13:02 >
# 5 Re: press ENTER at EditBox to execute
Hm.. IMHO , that has a pitfall. The edit control will get WM_KEYDOWN only if it has focus. If some other control has focus, then this logic will fail.

Well, it is an expected user experience in the windows world for the default button to provide that action whenever the Enter key is pressed. So, at any time, if you press enter ( except in the case of a multiline edit box ) , look for what button has the dark border, and it is that functionality that has to be executed. That's Windows GUI standard.
kirants at 2007-11-9 13:14:00 >
# 6 Re: press ENTER at EditBox to execute
I was implying that Dimension should also check for the keydown in the main DlgProc. That way, both are covered :)
tomcant at 2007-11-9 13:15:08 >
# 7 Re: press ENTER at EditBox to execute
Doesn't the edit control send a WM_NOTIFY message to the edit control's parent with code NM_RETURN when enter is pressed?

MSDN - NM_RETURN ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/common/notifications/nm_return.asp)
Zaccheus at 2007-11-9 13:16:02 >
# 8 Re: press ENTER at EditBox to execute
Well, it is an expected user experience in the windows world for the default button to provide that action whenever the Enter key is pressed.

That is the documented and standard truth, no question. But an amazingly large percent of computer lusers see to expect otherise.

I regularly have clients who INSIST that they need to press ENTER rather then TAB to move through the text boxes on a form.
TheCPUWizard at 2007-11-9 13:17:05 >
# 9 Re: press ENTER at EditBox to execute
You are right, NM_RETURN is sent to any controls parent window when the enter key is pressed, and in the form of a WM_NOTIFY message. Dont know why I didn't think of it in the first place! Ha :D
tomcant at 2007-11-9 13:18:12 >
# 10 Re: press ENTER at EditBox to execute
I am not saying anything against how to trap the enter key being pressed in the edit box. You guys have provided valid solutions.
However, the given problem is something else. It's not just about being able to trap enter key but to be able to execute a button functionality in response to that.

So, as a standard windows user, I always expect the functionality of the defalut button to be executed when I hit enter in a dialog. If I hit enter , and I see that some other function other than the "default push button" is getting executed, I will consider that to be a departure from Windows GUI navigation standard. The default push button property is there with a purpose.

What do you guys say ?
kirants at 2007-11-9 13:19:12 >
# 11 Re: press ENTER at EditBox to execute
Yeah, I think I know what you mean :)

But wouldn't it be simple just to handle NM_RETURN? That way the default functionality of the windows return key is kept. Or even simpler, just create the OK button with the BS_DEFPUSHBUTTON style.
tomcant at 2007-11-9 13:20:11 >
# 12 Re: press ENTER at EditBox to execute
I think in this situation, where the user has an owner drawn button, I don't know if the user has an indication of what the default button is. So, trapping the NM_RETURN may work out fine:thumb:

I was just trying to explain the possible ramifications of some of the methods so that the poster can use the best judegement in the given situation.
kirants at 2007-11-9 13:21:10 >