Thread: Prevent VK_RETURN from being processed in subclassed edit control.

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Question Prevent VK_RETURN from being processed in subclassed edit control.

    I have subclassed an Edit control for the purpose of detecting the enter key being pressed then calling a function accordingly. All that part seems to work just fine so far. The problem I am having is that I then want the Edit control to not move to the next line. I have tried simply returning 0 after calling my procedure that should trigger on each press of the enter key, but this does not seem to prevent the Edit box from moving down to the next line.

    This is what I have thus far:
    Code:
    // ...
    HWND g_hEdit;
    WNDPROC g_EditProc;
    // ...
    
    // Subclassed edit window procedure.
    LRESULT WINAPI EditProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    // Check for Enter key to start sending chunks.
    switch (msg)
    {
        case WM_KEYDOWN:
        switch (LOWORD(wParam))
        {
            case VK_RETURN:
            SendChat();
            break; // tried replacing with 'return 0;' and it did not have the desired effect.
        }
        break;
        case WM_CLOSE:
        SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
        break;
        case WM_DESTROY:
        SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
        break;
    }
    return CallWindowProc(g_EditProc, hWnd, msg, wParam, lParam);
    }
    
    // Main window procedure.
    LRESULT CALLBACK MainWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    RECT rcMain;
    
    switch (msg)
    {
        // Build child windows.
        case WM_CREATE:
        g_hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
        0, 0, 290, 170, hWnd, NULL, GetModuleHandle(NULL), NULL);
        g_EditProc = (WNDPROC)SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)EditProc);
        break;
    // ...
    I have searched online and found some examples on how to disable Ctrl+V and Ctrl+Alt+Del and the like. These seem to be a bit advanced and outside the scope of something that to me must be much simpler. I also found examples on how to prevent normal roman characters and numbers from being sent, they suggested using return 0. This solution doesn't seem to work in regards to Enter though.
    Last edited by hippiechic; 12-22-2012 at 12:37 AM. Reason: Garbled

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    You have to check for WM_CHAR and suppress the newline character(s) when they're sent. These message is generated by TranslateMessage, but the WndProc is called by DispatchMessage so assuming you have a normal message loop whatever you do in the WndProc is too late to stop it from happening.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    guestgulkan from cplusplus.com forums wrote:

    I tried your code and it does do as you say - so I thought the edit box
    might still be recieving the WM_CHAR with carriage return - so I did this:

    Code:
    // Subclassed edit window procedure.
    LRESULT WINAPI EditProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        // Check for Enter key to start sending chunks.
        switch (msg)
        {
        case WM_KEYDOWN:
            switch (LOWORD(wParam))
            {
            case VK_RETURN:
                SendChat();
                //break; // tried replacing with 'return 0;' and it did not have the desired effect.
                return 0;
            }
    
        case WM_CHAR:
            if (wParam == 0x0d) //check for carriage return
                return 0;
            break;
    
        case WM_CLOSE:
            SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
            break;
        case WM_DESTROY:
            SetWindowLong(g_hEdit, GWL_WNDPROC, (DWORD)g_EditProc);
            break;
        }
        return CallWindowProc(g_EditProc, hWnd, msg, wParam, lParam);
    }
    It seemed to do the trick for me.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Did you try the ES_WANTRETURN style? (requires ES_MULTILINE as well)

    BTW If you use WS_CHILD then you should cast the HMENU param in CreateWindow() to the controls ID.

    This would allow you to reduce your use of global variables, as you could then use many of the WIN32 API functions to get a control's HWND (ie GetDlgItem)

    Your using NULL which means all these controls created do not have a uneque ID and so may cause issues if created on the same window/dialog.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-23-2005, 08:00 AM
  2. Subclassed edit not doing what it's supposed to
    By tyouk in forum Windows Programming
    Replies: 8
    Last Post: 01-21-2005, 10:25 PM
  3. subclassed edit control trouble
    By ENF in forum Windows Programming
    Replies: 3
    Last Post: 10-27-2003, 03:59 AM
  4. Need help with edit control
    By XRIC++ in forum Windows Programming
    Replies: 3
    Last Post: 10-25-2003, 12:25 AM
  5. edit control???
    By Brita in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 12:05 PM

Tags for this Thread