Handling a WM_KEYDOWN message... - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Handling a WM_KEYDOWN message...

ShinGohanShinGohan Posts: 2Member
Hello, i'm new to MFC and i have got this problem:
i have a dialog with a CEdit control and i want to be able to handle a WM_KEYDOWN whenever i press a button while the editbox is focused..
i have added a "OnKeyDownText1(UINT nChar,UINT n_RepCnt,UINT nFlags)" handle in my dialog class and this (ON_WM_KEYDOWN(IDC_EDIT1,OnKeyDownText1)) in the message map, but it doesn't seem to work.. i've tried to trace the program and have found out that it never reaches my handler.. so how do i do it?

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : Hello, i'm new to MFC and i have got this problem:
    : i have a dialog with a CEdit control and i want to be able to handle a WM_KEYDOWN whenever i press a button while the editbox is focused..
    : i have added a "OnKeyDownText1(UINT nChar,UINT n_RepCnt,UINT nFlags)" handle in my dialog class and this (ON_WM_KEYDOWN(IDC_EDIT1,OnKeyDownText1)) in the message map, but it doesn't seem to work.. i've tried to trace the program and have found out that it never reaches my handler.. so how do i do it?
    :

    you need to subclass the CEdit control and handle it in that class. Create a new MFC class that is derived from CEdit, then in the dialog's .h file change the control's type from CEdit to your new class, for example CMyEdit.
  • ShinGohanShinGohan Posts: 2Member
    : : Hello, i'm new to MFC and i have got this problem:
    : : i have a dialog with a CEdit control and i want to be able to handle a WM_KEYDOWN whenever i press a button while the editbox is focused..
    : : i have added a "OnKeyDownText1(UINT nChar,UINT n_RepCnt,UINT nFlags)" handle in my dialog class and this (ON_WM_KEYDOWN(IDC_EDIT1,OnKeyDownText1)) in the message map, but it doesn't seem to work.. i've tried to trace the program and have found out that it never reaches my handler.. so how do i do it?
    : :
    :
    : you need to subclass the CEdit control and handle it in that class. Create a new MFC class that is derived from CEdit, then in the dialog's .h file change the control's type from CEdit to your new class, for example CMyEdit.
    :
    I tried it.. what i don't understand is: where do i write the event handler?
    I need it to be a Dialog member and not a CMyEdit member, because i have to access to other members defined in the dialog class.. if i just overload the CEdit's OnKeyDown() method i can't access something that is "external" to the class..
  • stoberstober Posts: 9,765Member ✭✭✭
    One way I solved that problem is for CMyEdit to reflect the WM_KEYDOWN to its parent (the CDialog derived class that contains the edit control) Here is the code snippet. Use ClassWizard to create both event handlers.


    [code]
    void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    ::PostMessage(GetParent()->m_hWnd, WM_KEYDOWN,nChar,0);
    CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
    }
    [/code]

    [code]
    void CTestDlgDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // TODO: Add your message handler code here and/or call default

    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
    }

    [/code]

Sign In or Register to comment.