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. CEdit And OnKeyDown [modified]

CEdit And OnKeyDown [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 2 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.
  • S Offline
    S Offline
    Schehaider_Aymen
    wrote on last edited by
    #1

    hi everybody, I m trying to capture a keypress from the user on a CEdit control (Edit TexT). So My Form is a CFormView Based and I have an Edit Texton it. I Added a WM_KEYDOWN ("OnKeyDown" function) via the Windows Message Wizard But still when i press a key nothing will happen here is my code

    void CMy65465View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // TODO: Add your message handler code here and/or call default
    switch(nChar)
    {
    case VK_RETURN:
    MessageBox("You pressed Enter");
    break;
    case VK_F1:
    MessageBox("Help is not available at the moment");
    break;
    case VK_DELETE:
    MessageBox("Can't Delete This");
    break;
    default:
    MessageBox("Whatever");
    }

    CFormView::OnKeyDown(nChar, nRepCnt, nFlags);
    

    }

    Where is the prob :omg:

    "The Ultimate Limit Is Only Your Imagination."

    modified on Tuesday, June 8, 2010 11:49 AM

    A 1 Reply Last reply
    0
    • S Schehaider_Aymen

      hi everybody, I m trying to capture a keypress from the user on a CEdit control (Edit TexT). So My Form is a CFormView Based and I have an Edit Texton it. I Added a WM_KEYDOWN ("OnKeyDown" function) via the Windows Message Wizard But still when i press a key nothing will happen here is my code

      void CMy65465View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
      {
      // TODO: Add your message handler code here and/or call default
      switch(nChar)
      {
      case VK_RETURN:
      MessageBox("You pressed Enter");
      break;
      case VK_F1:
      MessageBox("Help is not available at the moment");
      break;
      case VK_DELETE:
      MessageBox("Can't Delete This");
      break;
      default:
      MessageBox("Whatever");
      }

      CFormView::OnKeyDown(nChar, nRepCnt, nFlags);
      

      }

      Where is the prob :omg:

      "The Ultimate Limit Is Only Your Imagination."

      modified on Tuesday, June 8, 2010 11:49 AM

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      That key handler (provided you've got a message map entry for it) will only trap key downs for the form view. The edit control is a different window. Your choices here are: - find a WM_COMMAND from the edit control that does what you want and handle that in the form view - derive a class from CEdit and reflect messages from the form view into the control. Do a search for MFC message reflection to see how this works. (This[^] isn't too bad about message reflection but it might be out of date). Cheers, Ash

      S 1 Reply Last reply
      0
      • A Aescleal

        That key handler (provided you've got a message map entry for it) will only trap key downs for the form view. The edit control is a different window. Your choices here are: - find a WM_COMMAND from the edit control that does what you want and handle that in the form view - derive a class from CEdit and reflect messages from the form view into the control. Do a search for MFC message reflection to see how this works. (This[^] isn't too bad about message reflection but it might be out of date). Cheers, Ash

        S Offline
        S Offline
        Schehaider_Aymen
        wrote on last edited by
        #3

        i did add the WM_KDOWN but still nbothing happen when i press a key. I tried it with an application Based on CEditView and it works. It seems a prob with The FormView Class :((

        "The Ultimate Limit Is Only Your Imagination."

        A 1 Reply Last reply
        0
        • S Schehaider_Aymen

          i did add the WM_KDOWN but still nbothing happen when i press a key. I tried it with an application Based on CEditView and it works. It seems a prob with The FormView Class :((

          "The Ultimate Limit Is Only Your Imagination."

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          Isn't CEditView "just" and edit control? Any messages from the edit control will be processed by the CEditView. An edit control on a form view has an additional window involved. Key down message will be processed by the edit control and not the form view. Cheers, Ash

          S 2 Replies Last reply
          0
          • A Aescleal

            Isn't CEditView "just" and edit control? Any messages from the edit control will be processed by the CEditView. An edit control on a form view has an additional window involved. Key down message will be processed by the edit control and not the form view. Cheers, Ash

            S Offline
            S Offline
            Schehaider_Aymen
            wrote on last edited by
            #5

            Ok, what u said is great and means that my edit control, normally, can implement the OnKeyDow function even if my form is CFormView Class. but based on my code (first message) nothing is happening. :sigh:

            "The Ultimate Limit Is Only Your Imagination."

            A 1 Reply Last reply
            0
            • A Aescleal

              Isn't CEditView "just" and edit control? Any messages from the edit control will be processed by the CEditView. An edit control on a form view has an additional window involved. Key down message will be processed by the edit control and not the form view. Cheers, Ash

              S Offline
              S Offline
              Schehaider_Aymen
              wrote on last edited by
              #6

              what about PreTranslateMessage ?? how could i know the key pressed by the user ??

              "The Ultimate Limit Is Only Your Imagination."

              A 1 Reply Last reply
              0
              • S Schehaider_Aymen

                Ok, what u said is great and means that my edit control, normally, can implement the OnKeyDow function even if my form is CFormView Class. but based on my code (first message) nothing is happening. :sigh:

                "The Ultimate Limit Is Only Your Imagination."

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                Yep, but your code for the handler is in your CFormView overide, not in a CEdit override. What I'd do is... - Derive a class from CEdit - Add ON_WM_KEYDOWN to its message map - Implement OnKeyDown Snip, snip, Bob's yer Aunty. Cheers, Ash PS: If you do implement it this way remember to call CEdit::OnKeyDown to allow the normal window procedure to handle the keystroke otherwise you'll cripple the behaviour of the edit control.

                S 1 Reply Last reply
                0
                • S Schehaider_Aymen

                  what about PreTranslateMessage ?? how could i know the key pressed by the user ??

                  "The Ultimate Limit Is Only Your Imagination."

                  A Offline
                  A Offline
                  Aescleal
                  wrote on last edited by
                  #8

                  You don't need to use PreTranslateMessage - that's for doing something global to a message before it's processed by a window. Cheers, Ash

                  1 Reply Last reply
                  0
                  • A Aescleal

                    Yep, but your code for the handler is in your CFormView overide, not in a CEdit override. What I'd do is... - Derive a class from CEdit - Add ON_WM_KEYDOWN to its message map - Implement OnKeyDown Snip, snip, Bob's yer Aunty. Cheers, Ash PS: If you do implement it this way remember to call CEdit::OnKeyDown to allow the normal window procedure to handle the keystroke otherwise you'll cripple the behaviour of the edit control.

                    S Offline
                    S Offline
                    Schehaider_Aymen
                    wrote on last edited by
                    #9

                    It seems the best way, after all spending 4 hours looking for a function or a sample code is stupid ;P I created a CEdit based class, called CTest in whhich i add the OnKeyDown(...) Now how could call it from the OnChangeEdit1 Function of my CFormView? PS: I need to pass the 3 parameters UINT of OnKeyDown << Newbie question sorry

                    "The Ultimate Limit Is Only Your Imagination."

                    modified on Tuesday, June 8, 2010 1:36 PM

                    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