WM_KEYDOWN message
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: WM_KEYDOWN message

  1. #1
    Join Date
    Jul 2005
    Posts
    123

    WM_KEYDOWN message

    We Can map ON_WM_KEYDOWN message to view class, Similarly Can i map ON_WM_KEYDOWN message to my window which is derived from CWnd? I tried it, but i'm not getting message to my window.

    I want to map ON_WM_KEYDOWN message to my window. How to achieve it?

    -pkalp

  2. #2
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: WM_KEYDOWN message

    Your CView (I'm assuming you are referring to such a class) IS derived from CWnd.

    If you look at documentation for MFC-function OnKeyDown() you'll see that it IS a CWnd-function. Therefore, there should be no problem overriding it in any class you'd like derived from CWnd.

    Are you sure that when you say that the messages don't get to your CWnd-derived class, that this class has focus?

    Maybe if you posted some code, we could investigate some more. I think that what you're trying to achieve should be "a walk in the park".
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  3. #3
    Join Date
    Jul 2005
    Posts
    123

    Re: WM_KEYDOWN message

    I've two window (CChildView dervied from CWnd and CVSeriesWindow derived from CWnd). From CChildView, i'm creating object for CVSeriesWindow and placing that window in my CChildView.

    When i scroll my mouse, CMainFrame's OnMouseWheel() is called, from there i'm calling CChildView and then i'm passing it to CVSeriesWindow.

    When mouse scroll is working, then why key down message is not getting called.

    Am i wrong in mapping? What could be wrong?


    -pkalp

  4. #4
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: WM_KEYDOWN message

    Isn't it kind of odd to override OnMouseWheel(), or any other CWnd method for that matter, in CMainFrame class? That class is normally used to handle "non-client-area" messages, such as menu selections, caption hits etc. But, I'm no expert, so maybe it's fine...

    That leads me to my next question; Where is your WM_KEYDOWN message handler implemented? That is; In which class do you have your override of CWnd::OnKeyDown() implemented?

    If you have something like CVSeriesWindow::OnKeyDown(), AND this window has focus when you hit a (non system) key, the WM_KEYDOWN message should definitively end up in that handler. If it doesn't, I would have to see some code to try and pin-point better...
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: WM_KEYDOWN message

    As already stated, it sounds like your window is loosing focus when you scroll.


    does the message show up in PreTranslateMessage

    Code:
    BOOL FOO::PreTranslateMessage(MSG *pMSG)
    {
    	if (pMSG->message == WM_KEYDOWN)
    	     AfxMessageBox("BLAH");               
    	
    	return (BASE_FOO::PreTranslateMessage(pMSG));
    }
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  6. #6
    Join Date
    Jul 2005
    Posts
    123

    Re: WM_KEYDOWN message

    In CMainFrame, i receive mouse wheel message and i'm passing it to CChildView.

    In ChildView class, i created object for CVSeriesWindow.

    if(!m_pSeriesWindow)
    {
    m_pSeriesWindow = new CVSeriesWindow(this);
    }

    In CVSeriesWindow class constructor, i've created my window (CVSeriesWindow) using CWnd::Create() function.

    Focus is on CVSeriesWindow, I'm able to load images on CVSeriesWindow and it is visible. Then why OnKeyDown() is not called?

    void CVSeriesWindow::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    AfxMessageBox("key down");
    CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
    }


    What is wrong?

    -pkalp

  7. #7
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Wink Re: WM_KEYDOWN message

    It doesn't look like anything is wrong here. I'm still leaning towards the theory that focus somehow isn't where we expect it to be...

    Here's something to try out to determine for sure if focus is in your CVSeriesWindow: What if you implement your OnMouseWheel handler in the CVSeriesWindow instead of CMainFrame, just for testing? If CVSeriesWindow has focus when you turn the wheel, the message should get there without having to pass it from CMainFrame.

    Alternatively, you could make a temporary message handler for WM_MOUSEMOVE and check if that one is called when you move the mouse over your CVSeriesWindow.

    What if you click in the CVSeriesWindow with the mouse just prior to pressing a key? Clicking in a window sets focus to that window.

    Another option is to use CWnd::GetFocus() to determine which window actually has focus. Trick is to find a good spot to call it. Maybe your CMainFrame implementation of OnMouseWheel() could be a reasonable place?

    Just throwing out ideas/tips here, because the code you posted I think should work just fine. Nothing spooky there as far as I can see...
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  8. #8
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Angry Re: WM_KEYDOWN message

    UPPSSS.....
    Remembered just after posting that WM_MOUSEMOVE doesn't require focus to get passed to the window the mouse is over. That message is therefore useless to use for checking focus. I think, however, that WM_MOUSEWHEEL require focus of the window it's getting passed to.

    Sorry...
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  9. #9
    Join Date
    Jul 2005
    Posts
    123

    Re: WM_KEYDOWN message

    Actually i've a window(CVSeriesWindow). In CVSeriesWindow, i placed objects of CVImageWindow (derived from CWnd) in the form of grid. What i need is to scroll using keyboard (up and down key) to see the remaining CVImageWindows in my window.

    When i click in the CVSeriesWindow with the mouse, CVImageWindow's OnLButtonDown() is called. When i scroll with the mouse, CMainFrame's OnMouseWheel() is called. But when i press key, nothing is called.

    How to scroll and see the image window when key press message is not called? Is there any method to invoke that event?


    -pkalp

  10. #10
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: WM_KEYDOWN message

    Quote Originally Posted by pkalp
    When i click in the CVSeriesWindow with the mouse, CVImageWindow's OnLButtonDown() is called.
    This leads me to believe that focus is in CVImageWindow and NOT in CVSeriesWindow, where you're keydown-handler is located. Have you tried to implement a key down handler in CVImageWindow, and see if it is called there when you press a key? Since your two windows are not related you cannot expect the handler in CVSeriesWindow to be called if focus in fact is on CVImageWindow...

    If you want to ensure operations on CVImageWindow to be handled in CVSeriesWindow, I would suggest deriving CVImageWindow from CVSeriesWindow if it is possible. Then, CVSeriesWindow would technically have focus also if CVImageWindow has it because CVImageWindow in this case IS a CVSeriesWindow.

    Good luck
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  11. #11
    Join Date
    Jul 2005
    Posts
    123

    Re: WM_KEYDOWN message

    I tried by keeping key down handler in CVImageWindow also, but nothing is called when i press key. CVSeriesWindow key down handler is also not called.

    Is there any method to invoke key down event.


    -pkalp

  12. #12
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: WM_KEYDOWN message

    Well, I'm running out of ideas here... Maybe one last "shot from the hip";
    Can you trap the WM_KEYDOWN message somewhere else? Maybe in your CChildview or CMainFrame, and pass it on to your desired window by means of PostMessage or SendMessage, or perhaps just call OnKeyDown directly for your desired class. Guess you have a CVSeriesWindow object in CChildView that could be used to invoke the CVSeriesWindow::OnKeyDown().

    It's an odd approach, but it might work. Point is that we need to locate the WM_KEYDOWN message. Some window gets it. Pretty sure of that, unless you find yourself in a situation where no window actually has focus. Not sure how that can happen, but it is referred to as a possible state in MSDN, so I guess it can happen.
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

  13. #13
    Join Date
    Jul 2005
    Posts
    123

    Re: WM_KEYDOWN message

    None of my window is calling OnKeyDown().

    When OnLButtonDown() is called in CVImageWindow, why this OnKeyDown() is not called. If my message is called in OnKeydown(), then no prob, i can pass it to CVSeriesWindow.

    Nothing is called when i press key.


    -pkalp

  14. #14
    Join Date
    Jul 2005
    Posts
    123

    Re: WM_KEYDOWN message

    None of my window is calling when i press key.

    When OnLButtonDown() is called in CVImageWindow, why this OnKeyDown() is not called. If my message is called in OnKeydown(), then no prob, i can pass it to CVSeriesWindow.

    Nothing is called when i press key.


    -pkalp

  15. #15
    Join Date
    Aug 2002
    Location
    Covington, LA, USA
    Posts
    101

    Re: WM_KEYDOWN message

    There is a distinct difference when pressing a mouse buttonn and a keyboard key. When a mouse button is pressed, the window in which it is pressed GETS the focus I think. A keyboard key press is sent to the whatever window has focus at that time. I.e. there is necessarily no coupling between say the location of the mouse cursor and which window has focus.

    When yo say that "None of your windows are calling when you're pressing key", do you have WM_KEYDOWN handlers in place in "all" of your windows to verify this?

    I'd suggest you take som time and try to find out which window has focus. Use GetFocus(). If none has focusm, this returns NULL. If not it returns a pointer to the CWnd that has it. If you then use GetClassName() for example you can find out which of your classes has it. For debug purposes it might be an idea to call GetFocus in a timer function or something, to ensure that the function is called. If you do a TRACE of the class name, you can monitor in your output window which classes has focus as your program runs.

    Sorry, I don't have a solution, can only give tips on how and where to start looking. Let me know if you get any closer...
    "Soccer - it is not a matter of life and death - it's far more important than that" Bill Shankly

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center