Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

win32 c++ detecting 'enter' in a edit control withot subclassing?

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

like image 420
KaiserJohaan Avatar asked Jan 02 '11 22:01

KaiserJohaan


3 Answers

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.

like image 109
kellogs Avatar answered Oct 03 '22 07:10

kellogs


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.

like image 32
jussij Avatar answered Oct 03 '22 07:10

jussij


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.

like image 27
Mike Kwan Avatar answered Oct 03 '22 07:10

Mike Kwan