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. mr MFC please do something when I press a key

mr MFC please do something when I press a key

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
6 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.
  • S Offline
    S Offline
    Spiritofamerica
    wrote on last edited by
    #1

    hello I am making a program in MFC I have a class for a window I want to achieve this: when a key is pressed something happens. To achieve this I did this I went to class view I right clicked on my class and clicked on propreties I clciked on messages I selected WM_KEYDOWN and from the right column I selected Add OnKeyDown this created a new function: void CViewListaLocatari::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { MessageBox("toader","toader",MB_OK); if (nChar==VK_DELETE) OnBnSterge(); else CFormView::OnKeyUp(nChar, nRepCnt, nFlags); CFormView::OnKeyDown(nChar, nRepCnt, nFlags); } and this line appeared in the message map ON_WM_KEYDOWN() I changed it to this ON_WM_KEYDOWN(WM_KEYDOWN,OnKeyDown) because it made more sense for it to be like that although I don't know if I did right anyway it doesn't work and no messagebox appears even how do I make it work? or do you know another way to do this?

    S R 2 Replies Last reply
    0
    • S Spiritofamerica

      hello I am making a program in MFC I have a class for a window I want to achieve this: when a key is pressed something happens. To achieve this I did this I went to class view I right clicked on my class and clicked on propreties I clciked on messages I selected WM_KEYDOWN and from the right column I selected Add OnKeyDown this created a new function: void CViewListaLocatari::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { MessageBox("toader","toader",MB_OK); if (nChar==VK_DELETE) OnBnSterge(); else CFormView::OnKeyUp(nChar, nRepCnt, nFlags); CFormView::OnKeyDown(nChar, nRepCnt, nFlags); } and this line appeared in the message map ON_WM_KEYDOWN() I changed it to this ON_WM_KEYDOWN(WM_KEYDOWN,OnKeyDown) because it made more sense for it to be like that although I don't know if I did right anyway it doesn't work and no messagebox appears even how do I make it work? or do you know another way to do this?

      S Offline
      S Offline
      Stober
      wrote on last edited by
      #2

      First -- don't screw around with the code that ClassWizard writes unles you know what you are doing. Change it back the way it was. WM_KEYDOWN events are not handled by CDialog-derived classes. You have to use PreTranslateMessage() to capture the key events. Key events always go to the window that has focus.

      M S 2 Replies Last reply
      0
      • S Spiritofamerica

        hello I am making a program in MFC I have a class for a window I want to achieve this: when a key is pressed something happens. To achieve this I did this I went to class view I right clicked on my class and clicked on propreties I clciked on messages I selected WM_KEYDOWN and from the right column I selected Add OnKeyDown this created a new function: void CViewListaLocatari::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { MessageBox("toader","toader",MB_OK); if (nChar==VK_DELETE) OnBnSterge(); else CFormView::OnKeyUp(nChar, nRepCnt, nFlags); CFormView::OnKeyDown(nChar, nRepCnt, nFlags); } and this line appeared in the message map ON_WM_KEYDOWN() I changed it to this ON_WM_KEYDOWN(WM_KEYDOWN,OnKeyDown) because it made more sense for it to be like that although I don't know if I did right anyway it doesn't work and no messagebox appears even how do I make it work? or do you know another way to do this?

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        Have you tried catching WM_CHAR ?

        S 1 Reply Last reply
        0
        • S Stober

          First -- don't screw around with the code that ClassWizard writes unles you know what you are doing. Change it back the way it was. WM_KEYDOWN events are not handled by CDialog-derived classes. You have to use PreTranslateMessage() to capture the key events. Key events always go to the window that has focus.

          M Offline
          M Offline
          Michael Kane
          wrote on last edited by
          #4

          Thank you. It works fine.

          1 Reply Last reply
          0
          • S Stober

            First -- don't screw around with the code that ClassWizard writes unles you know what you are doing. Change it back the way it was. WM_KEYDOWN events are not handled by CDialog-derived classes. You have to use PreTranslateMessage() to capture the key events. Key events always go to the window that has focus.

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

            can you please elaborate on this PreTranslateMessage() where exactly should I put this ? It is not a method that I can use can you please explain more please

            1 Reply Last reply
            0
            • R Rick York

              Have you tried catching WM_CHAR ?

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

              Rick York wrote: Have you tried catching WM_CHAR ? doesn't work

              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