Capture Enter key press in EDIT Box

This is a discussion on Capture Enter key press in EDIT Box within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I'm rather new to win32 programming and am having a hard time trying to work out how to ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Capture Enter key press in EDIT Box

    Hi all,

    I'm rather new to win32 programming and am having a hard time trying to work out how to capture when the ENTER key has been pressed in an EDIT box.
    My main window has a few controls on it:
    - a listview
    - 2 buttons
    - an edit box

    The edit box is a single line box that takes text and will search the listview for matching strings. One of the 2 buttons is a "Search" button and the other is a "Clear Search" button. My search functionality works but only when the "Search" button is pressed, but I want it so that if you type in some text in the edit box and press enter it will run the search function.

    How do I catch when the enter key has been pressed in the edit box?
    Do I catch a Windows event in my WindowProcedure() function?

    Some of my code is:

    Code:
    LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_CREATE:
            {
                 // some code to handle window create
            }
            case WM_COMMAND:
            {
    		    HANDLE_WM_COMMAND(hwnd, wParam, lParam, onCommand);
    		    break;
            }
            case WM_DESTROY:
            {
                PostQuitMessage(0);
                break;
            }
            default:
            {
                return DefWindowProc(hwnd, message, wParam, lParam);
            }
        }
        return 0;
    }
    
    void onCommand(HWND hwnd, int id, HWND hCtl, UINT codeNotify)
    {
    	switch(id)
    	{
            case IDB_BUTTON:
            {
                switch (HIWORD(id))
                {
                    case BN_CLICKED:
                    {
                         // handle "Search" button click, run search function
                    }
                }
                break;
            }
    }
    Thanks in advance.

  2. #2
    Registered User r1ck0r's Avatar
    Join Date
    Apr 2005
    Location
    UK
    Posts
    30
    Whenever I do this I usually just subclass the edit control and capture the WM_CHAR Message.
    However, I believe you can also give the edit control the ES_MULTILINE style, and make the search button the default button, and it will do it for you automatically.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Quote Originally Posted by r1ck0r
    Whenever I do this I usually just subclass the edit control and capture the WM_CHAR Message.
    However, I believe you can also give the edit control the ES_MULTILINE style, and make the search button the default button, and it will do it for you automatically.
    Sorry, what do you mean subclass? I'm writing my code in C (not C++) as I don't really know C++ yet.
    If I change the EDIT control to ES_MULTILINE how do I make the Search button the default button? I'm creating my Window (and the Button) via code and not a resource file.
    Thanks in advance.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263
    Subclassing means that you effectively "take control" of the edit box in order to make it react in the way you want it to. You're familiar with making a WindowProc for your window, right? This is very similar, you make a WindowProc and replace the edit box's default WindowProc so you can process the messages it receives.

    Start with:
    Code:
    //put this with your globals at the top of your code
    WNDPROC DefEditProc;
    
    //Then add this template
    LRESULT EditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch (uMsg)
    	{
    		default:
    			return CallWindowProc(DefEditProc, hwnd, uMsg, wParam, lParam);
    
    	}
    
    	return FALSE;
    }
    There ya go, that'll be your initial WindowProc. To replace the default WindowProc with it, use:-
    Code:
    DefEditProc = (WNDPROC)SetWindowLong(hwndEdit, GWL_WNDPROC, (long)EditProc);
    Where hwndEdit is your Edit box's window handle.

    WARNING
    Make sure that you replace the WindowProc before your program exits! Otherwise bad things may happen. Do this via:-
    Code:
    SetWindowLong(hwndEdit, GWL_WNDPROC, (long)DefEditProc);

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    SMurf, thanks heaps for your help, that is exactly what I needed. Now I know about subclassing I can use this code (and concept) elsewhere.

    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. "press any key to continue" without the enter key
    By chickenandfries in forum C Programming
    Replies: 1
    Last Post: 03-29-2008, 09:56 PM
  3. Replies: 9
    Last Post: 02-13-2008, 01:59 PM
  4. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  5. Dialog Edit Control and Enter Key
    By Quantrizi in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2004, 07:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21