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. Capture Keyboard Input

Capture Keyboard Input

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
8 Posts 5 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
    sweep123
    wrote on last edited by
    #1

    Can someone tell me how to capture key presses when they are being entered into a Text box. I did add the ON_WM_KEYUP and OM_WM_KEYDOWN messages but breakpoints in there procedures (OnkeyDown etc) were never reached.

    A D 2 Replies Last reply
    0
    • S sweep123

      Can someone tell me how to capture key presses when they are being entered into a Text box. I did add the ON_WM_KEYUP and OM_WM_KEYDOWN messages but breakpoints in there procedures (OnkeyDown etc) were never reached.

      A Offline
      A Offline
      Andrew Quinn AUS
      wrote on last edited by
      #2

      Hi Sweep, One way is to use PreTranslateMessage virtual function, e.g.

      BOOL CMyWindow::PreTranslateMessage(MSG* pMsg)
      {
      BOOL bEatMe = FALSE;

      if (pMsg && pMsg->message == WM\_KEYDOWN && pMsg->hwnd == m\_edit1.GetSafeHwnd())
      {
      	BOOL bShift = (GetKeyState(VK\_SHIFT) < 0);
      	BOOL bCtrl = (GetKeyState(VK\_CONTROL) < 0);
      	BOOL bAlt = (pMsg->message == WM\_SYSKEYDOWN);
      
      	// pMsg->wParam holds the keycode...
      
      	bEatMe = FALSE; // FALSE if you want the key to be processed 
      }
      
      // return if we've processed this message or want the default/system to
      return (bEatMe) ? TRUE:CDialog::PreTranslateMessage(pMsg);
      

      }

      If you are trying to do clever things, my advice would be to create a class (derived from CEdit) and use this in your views/dialogs. Hope this helps, Andy

      1 Reply Last reply
      0
      • S sweep123

        Can someone tell me how to capture key presses when they are being entered into a Text box. I did add the ON_WM_KEYUP and OM_WM_KEYDOWN messages but breakpoints in there procedures (OnkeyDown etc) were never reached.

        D Offline
        D Offline
        Diddy
        wrote on last edited by
        #3

        Depends what you want to capture. If your only intrested in 0 > 9, a > z, A > Z plus a few others like VK_RETURN, VK_ESCAPE etc - the easist way is to add a ON_WM_CHAR handler to your text box class. Anything that has its WM_KEYUP/WM_KEYDOWN translated to a WM_CHAR will be caught this way. Though - your ON_WM_KEYUP/DOWN handlers should work. Is this text box part of a dialog? Is it being subclased correctly, probably via a DDX_Control?

        S 1 Reply Last reply
        0
        • D Diddy

          Depends what you want to capture. If your only intrested in 0 > 9, a > z, A > Z plus a few others like VK_RETURN, VK_ESCAPE etc - the easist way is to add a ON_WM_CHAR handler to your text box class. Anything that has its WM_KEYUP/WM_KEYDOWN translated to a WM_CHAR will be caught this way. Though - your ON_WM_KEYUP/DOWN handlers should work. Is this text box part of a dialog? Is it being subclased correctly, probably via a DDX_Control?

          S Offline
          S Offline
          sweep123
          wrote on last edited by
          #4

          The Edit box has not been sub-classed, I assume I need to do that to get the WM_KEYUP/WM_KEYDOWN working. I just ant to trap the Tab and Return keys.

          D 1 Reply Last reply
          0
          • S sweep123

            The Edit box has not been sub-classed, I assume I need to do that to get the WM_KEYUP/WM_KEYDOWN working. I just ant to trap the Tab and Return keys.

            D Offline
            D Offline
            Diddy
            wrote on last edited by
            #5

            lol. Would be them two wouldn't it. Well, your KEYUP/DOWN handler will and should work for all other keys. Not however, Tab and return, which are dialog box navigation keys. The system handles these in a different way "The WM_GETDLGCODE message is sent to the window procedure associated with a control. By default, the system handles all keyboard input to the control; the system interprets certain types of keyboard input as dialog box navigation keys. To override this default behavior, the control can respond to the WM_GETDLGCODE message to indicate the types of input it wants to process itself." In other words, you'll never get a KEYUP/DOWN for Tab/return as you get a WM_GETDLGCODE which returns DLGC_DEFPUSHBUTTON/DLGC_WANTTAB depending

            S 1 Reply Last reply
            0
            • D Diddy

              lol. Would be them two wouldn't it. Well, your KEYUP/DOWN handler will and should work for all other keys. Not however, Tab and return, which are dialog box navigation keys. The system handles these in a different way "The WM_GETDLGCODE message is sent to the window procedure associated with a control. By default, the system handles all keyboard input to the control; the system interprets certain types of keyboard input as dialog box navigation keys. To override this default behavior, the control can respond to the WM_GETDLGCODE message to indicate the types of input it wants to process itself." In other words, you'll never get a KEYUP/DOWN for Tab/return as you get a WM_GETDLGCODE which returns DLGC_DEFPUSHBUTTON/DLGC_WANTTAB depending

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

              So what about Andy's solution of using PreTranslateMessage, would that work? Or I am stuck!?!

              A D 2 Replies Last reply
              0
              • S sweep123

                So what about Andy's solution of using PreTranslateMessage, would that work? Or I am stuck!?!

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

                What they are trying to say is some other alternatives are error prone, subclass your CEdit is better like this article http://www.codeproject.com/editctrl/NumEditCtl.asp Sonork 100.41263:Anthony_Yio

                1 Reply Last reply
                0
                • S sweep123

                  So what about Andy's solution of using PreTranslateMessage, would that work? Or I am stuck!?!

                  D Offline
                  D Offline
                  dart13
                  wrote on last edited by
                  #8

                  The solution Andy Q gave you should work with tab and enter keys. I used a similar technique to trap Enter key in a form view. BOOL CMyFormView::PreTranslateMessage(MSG* pMsg) { // keyboard input for 'enter' and 'esc' if(pMsg->message==WM_KEYDOWN) { if(pMsg->wParam == 0x0d) // if Enter pressed { //your handler return TRUE; } else if(pMsg->wParam == 0x1b) // if Esc pressed { //... return TRUE; } } return CFormView::PreTranslateMessage(pMsg); }

                  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