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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// ...
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.
Try to process WM_CHAR message, at least this is the approach I found on google.
http://cboard.cprogramming.com/windows-programming/67604-capture-enter-key-press-edit-box.html

If the only problem you have is moving the cursor next line, just save the text in a buffer and send it again to the edit control after pressing ENTER key using EM_REPLACESEL message:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb761633(v=vs.85).aspx
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 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.
Thank you so much for figuring this out. I just got back to the computer after having become frustrated and having to walk away for a bit and find that you guys solved the problem. ^^
Mind you I jst thought - if we always recieve the WM_CHAR - we can ignore the WK_RETURN code and just use the WM_CHAR code - ike this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 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
            {
                SendChat();
                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);
}


Topic archived. No new replies allowed.