Basically I want an Enter to trigger a message I can catch when a edit control har focus and a user press enter. All solutions online seems to be about subclassing, but I was wondering if there was another way around it?
For example, my button has an identifier ID_BUTTON_SEND. Here's how I imagine it;
case WM_COMMAND:
switch (LOWORD(wParam))
case ID_BUTTON_SEND
if ('enter was pressed')
do this
else
default
...you get the idea :) I've read the http://support.microsoft.com/kb/102589 but frankly option 1 dosn't make much sense to me.
Cheers
Best way to catch this is before TranslateMessage gets called. So, if using MFC, override CWnd::PreTranslateMessage. If using only Win API, then just check in your message pump what the message contains before the call to TranslateMessage.
You could trap the focus change event and when the edit control gets the focus event just change the dialog default button to be the *ID_BUTTON_SEND* button. Then when the focus is lost remove this default button flag.
That would means that whenever the user hits enter when the edit control has the foucs the dialog would automatically fire the *ID_BUTTON_SEND* default button.
You can make the button the default button by adding the BS_DEFPUSHBUTTON to the GWL_STYLE of the button.
Just to reiterate upon the KB article. For option 1 you can actually simply handle IDOK in WM_COMMAND.
case WM_COMMAND:
if(wParam == IDOK){
ENTER WAS PRESSED
}else{
REGULAR EVENT HANDLING
}
This is a much easier and cleaner way to check for the Enter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With