CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2007
    Posts
    54

    Mouse messages work fine, keybord don't ?

    'lo all,

    I have a view with a few OnMouseWhatever() handlers set up, all of these work fine. But keyboard handlers (OnChar(), OnKeyDown()...) don't seem to work ! It seems like the handler function is never called. Any idea why ?

    Thanks in advance.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Mouse messages work fine, keybord don't ?

    Where and how did you implement the"OnChar(), OnKeyDown()..." handlers that "don't seem to work"?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2007
    Posts
    54

    Re: Mouse messages work fine, keybord don't ?

    Quote Originally Posted by VictorN
    Where and how did you implement the"OnChar(), OnKeyDown()..." handlers that "don't seem to work"?
    Sorry, I guess I was a bit vague.

    My view (let's call it CMyView) is derived from CFormView. The handlers are defined as usual, i.e. "afx_msg void OnChar(params);" in the header, "ON_WM_CHAR()" in the message map, and the implementation "void CMyView::OnChar(params) { TRACE("test\n"); __super::OnChar(params); }" in the source file.

    I've put a breakpoint in the function, so I'm pretty sure the function is never actually called. I've also tried OnKeyDown() and OnKeyUp() instead of OnChar(), just to be sure. None work.

    The mouse handlers (OnMouseMove(), OnMouseWheel(), etc.) are defined in the same fashion, and they work like a charm.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Mouse messages work fine, keybord don't ?

    Well, all these messages are redirected to the control currently having focus. Therefore, you cannot handle them in the CFormView derived class.
    As a workaround (if you don't want handle them in the child controls' classes) you could add the virtual method PreTranslateMessage and handle WM_CHAR, WM_KEXDOWN, ... in it:
    Code:
    BOOL CMyView::PreTranslateMessage(MSG* pMsg)
    {
          UINT msg = pMsg->message;
          if (msg == WM_KEYDOWN)
          {
                //handle it
                ...
          }
          else if(   msg == WM_KEYUP)
          {
                //handle it
                ...
          }
          else if(   msg == WM_CHAR)
          {
                //handle it
                ...
          }
           //   return TRUE if you don't want this message to be processed somewere else
           // or call the base class:
           return CFormView::PreTranslateMessage(pMsg);
    }
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2007
    Posts
    54

    Re: Mouse messages work fine, keybord don't ?

    I'll try that. Thanks a lot!

  6. #6
    Join Date
    Feb 2007
    Posts
    54

    Re: Mouse messages work fine, keybord don't ?

    Alright, problem solved! Thanks a lot VictorN!

    Code:
    BOOL CMyView::PreTranslateMessage(MSG* pMsg)
    {
        UINT msg = pMsg->message;
        if (msg == WM_CHAR)
        {
            UINT nChar = pMsg->wParam;
            UINT nRepCnt = LOWORD(pMsg->lParam);
            UINT nFlags = HIWORD(pMsg->lParam);
    
            OnCharLikeFunction(nChar, nRepCnt, nFlags);
         }
    
        return __super::PreTranslateMsg(pMsg);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured