Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to send WM_KEYDOWN message?

How to send WM_KEYDOWN message?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #1

    Hi, I have a dialog which contains different controls,and I want to send a WM_KEYDOWN message to the parent dialog when a control receives a WM_KEYDOWN message. In the dialog,I overrided the OnKeyDown() method as follow:

    void Frm_WKFailureMatrix::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // TODO: Add your message handler code here and/or call default
    CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
    }

    and in the control,I also overrided the OnKeyDown() method , and inside that handler I call the ::SendMessage(...) method,as follow:

    void WkMfcButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // TODO: Add your message handler code here and/or call default
    int x = 0;
    ::SendMessage(GetOwner()->GetSafeHwnd() , WM_KEYDOWN , 0 , 0);
    }

    My problem is that ::SendMessage() accept the third and fourth parameters as WPARAM and LPARAM(UINT_PTR and LONG_PTR). How can I convert nChar,nRepCnt and nFlags to WPARAM and LPARAM so the parent dialog will receive the correct parameters??? With best regards, Eli

    T R 2 Replies Last reply
    0
    • E eli15021979

      Hi, I have a dialog which contains different controls,and I want to send a WM_KEYDOWN message to the parent dialog when a control receives a WM_KEYDOWN message. In the dialog,I overrided the OnKeyDown() method as follow:

      void Frm_WKFailureMatrix::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
      {
      // TODO: Add your message handler code here and/or call default
      CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
      }

      and in the control,I also overrided the OnKeyDown() method , and inside that handler I call the ::SendMessage(...) method,as follow:

      void WkMfcButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
      {
      // TODO: Add your message handler code here and/or call default
      int x = 0;
      ::SendMessage(GetOwner()->GetSafeHwnd() , WM_KEYDOWN , 0 , 0);
      }

      My problem is that ::SendMessage() accept the third and fourth parameters as WPARAM and LPARAM(UINT_PTR and LONG_PTR). How can I convert nChar,nRepCnt and nFlags to WPARAM and LPARAM so the parent dialog will receive the correct parameters??? With best regards, Eli

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      static_cast<>()...


      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      J 1 Reply Last reply
      0
      • E eli15021979

        Hi, I have a dialog which contains different controls,and I want to send a WM_KEYDOWN message to the parent dialog when a control receives a WM_KEYDOWN message. In the dialog,I overrided the OnKeyDown() method as follow:

        void Frm_WKFailureMatrix::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
        {
        // TODO: Add your message handler code here and/or call default
        CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
        }

        and in the control,I also overrided the OnKeyDown() method , and inside that handler I call the ::SendMessage(...) method,as follow:

        void WkMfcButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
        {
        // TODO: Add your message handler code here and/or call default
        int x = 0;
        ::SendMessage(GetOwner()->GetSafeHwnd() , WM_KEYDOWN , 0 , 0);
        }

        My problem is that ::SendMessage() accept the third and fourth parameters as WPARAM and LPARAM(UINT_PTR and LONG_PTR). How can I convert nChar,nRepCnt and nFlags to WPARAM and LPARAM so the parent dialog will receive the correct parameters??? With best regards, Eli

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        Why are you doing such a messy thing? This way, you will be writing keydown handlers for each and every control. Do you want your code to look like Sphagetti? What if you add two new controls later? Did I not ask you to override the PreTranslateMessage()? Did you ever try that?

        E 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          Why are you doing such a messy thing? This way, you will be writing keydown handlers for each and every control. Do you want your code to look like Sphagetti? What if you add two new controls later? Did I not ask you to override the PreTranslateMessage()? Did you ever try that?

          E Offline
          E Offline
          eli15021979
          wrote on last edited by
          #4

          Hi brahmma, I know the way of overriding PreTranslateMessage. The problem is that I've put a breakpoint inside that function but I never stop there. My code is :

          virtual BOOL PreTranslateMessage(MSG\* pMsg);
          

          and the implementation is :

          BOOL Frm_WKFailureMatrix::PreTranslateMessage(MSG* pMsg)
          {
          if (pMsg->message == WM_KEYDOWN)
          {
          int x = 0;
          }
          return TRUE;
          }

          However,if i move the focus into a button(by mouse click),the button receives the WM_KEYDOWN message. 1. Is it because of that automatically when I select a window , the focus is moving into the first control inside that window? 2. As far as I understand , the parrent windw shall receive the WM_KEYDOWN message and dispatch it to all of its children , isn't it??? Thanks again, Eli

          R 1 Reply Last reply
          0
          • T toxcct

            static_cast<>()...


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            J Offline
            J Offline
            JudyL_MD
            wrote on last edited by
            #5

            toxcct wrote:

            static_cast<>()...

            Actually, that won't work correctly. The OP actually needs to pack the three parameters back into the wParam and lParam. The MAKELPARAM and MAKEWPARAM macros will do this. Refer to the WM_KEYDOWN doc to determine where each item goes. Judy

            T 1 Reply Last reply
            0
            • J JudyL_MD

              toxcct wrote:

              static_cast<>()...

              Actually, that won't work correctly. The OP actually needs to pack the three parameters back into the wParam and lParam. The MAKELPARAM and MAKEWPARAM macros will do this. Refer to the WM_KEYDOWN doc to determine where each item goes. Judy

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              my bad, i read too fast


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              1 Reply Last reply
              0
              • E eli15021979

                Hi brahmma, I know the way of overriding PreTranslateMessage. The problem is that I've put a breakpoint inside that function but I never stop there. My code is :

                virtual BOOL PreTranslateMessage(MSG\* pMsg);
                

                and the implementation is :

                BOOL Frm_WKFailureMatrix::PreTranslateMessage(MSG* pMsg)
                {
                if (pMsg->message == WM_KEYDOWN)
                {
                int x = 0;
                }
                return TRUE;
                }

                However,if i move the focus into a button(by mouse click),the button receives the WM_KEYDOWN message. 1. Is it because of that automatically when I select a window , the focus is moving into the first control inside that window? 2. As far as I understand , the parrent windw shall receive the WM_KEYDOWN message and dispatch it to all of its children , isn't it??? Thanks again, Eli

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                if(pMsg->message == WM_KEYDOWN)
                {
                if(pMsg->wParam == VK_ESCAPE)
                AfxMessageBox(_T("You pressed the Escape Key"));
                }

                This is just an example. Use the virtual key codes to verify which key was pressed. Check out MSDN for virtual key codes.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups