Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
292 views
Welcome To Ask or Share your Answers For Others

1 Answer

The intended way for you to do this is to handle the WM_GETDLGCODE message and tell the Window manager that the edit control does not want to handle the TAB key. Raymond Chen covers this very issue in this article: Those who do not understand the dialog manager are doomed to reimplement it, badly. As often the case with Raymond, quite a provocative title to the article.

The code sample from the article looks like this:

LRESULT CALLBACK SubclassWndProc(
    HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  LRESULT lres;
  switch (uMsg) {
  case WM_GETDLGCODE:
    lres = CallWindowProc(...);
    lres &= ~DLGC_WANTTAB;
    if (lParam &&
        ((MSG *)lParam)->message == WM_KEYDOWN &&
        ((MSG *)lParam)->wParam == VK_TAB) {
      lres &= ~DLGC_WANTMESSAGE;
    }
    return lres;
  }
  return CallWindowProc(...);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...