Search code examples
cwinapi

How to eat keys in WM_KEYDOWN


Im handling the WM_KEYDOWN message in an edit box.
I am handling a bunch of keys, but for certain keys (eg. tab) i'd like to prevent the displayable character from being appended to the editbox.

case WM_KEYDOWN:
    {
        switch(wParam)
        {
        case VK_TAB:
            //handle tab here
            //Eat tab key
            return 0;
        default:
            return DefWndProc(hwnd,message,wParam,lParam);
        }
     }
     break;

I can detect and delete the tab in WM_KEYUP message, but with this method the tab key is visibly added and removed from the edit box.

is there any way of eating a key in WM_KEYDOWN?


Solution

    1. Override PreTranslateMessage in your dialog class.
    2. If the MSG parameter's message is WM_KEYDOWN and the wParam is VK_TAB and the hwnd is that of your edit control, handle it and simply return TRUE.

    Returning TRUE means that that event has already been processed and thus will not be processed by the CEdit control.