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. Trapping WM_KEYDOWN and WM_KEYUP on entering text in edit control

Trapping WM_KEYDOWN and WM_KEYUP on entering text in edit control

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 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.
  • E Offline
    E Offline
    EvScott
    wrote on last edited by
    #1

    Hi, I need to carry out actions when an individual character is entered into a WIN32 C Edit control. These actions will take place each time a key is pressed and each time a key is released. Characters will be entered into an Edit control, but the edit control does not know when a keydown/up message is needed, or an onchar. I would appreciate some help in being able to 'trap' a keydown and keyup event when text is entered in an edit control. Thanks

    S 1 Reply Last reply
    0
    • E EvScott

      Hi, I need to carry out actions when an individual character is entered into a WIN32 C Edit control. These actions will take place each time a key is pressed and each time a key is released. Characters will be entered into an Edit control, but the edit control does not know when a keydown/up message is needed, or an onchar. I would appreciate some help in being able to 'trap' a keydown and keyup event when text is entered in an edit control. Thanks

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      You can subclass[^] the edit control, to process the Windows messages you want to, before passing them on to the edit control. If you're using MFC, derive from the edit control class[^] to do this.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      E 1 Reply Last reply
      0
      • S Stuart Dootson

        You can subclass[^] the edit control, to process the Windows messages you want to, before passing them on to the edit control. If you're using MFC, derive from the edit control class[^] to do this.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        E Offline
        E Offline
        EvScott
        wrote on last edited by
        #3

        Thanks Stuart, but I'm using windows 32 API functions and not MFC. Any ideas?

        S 1 Reply Last reply
        0
        • E EvScott

          Thanks Stuart, but I'm using windows 32 API functions and not MFC. Any ideas?

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Yes - the first link[^] in my message showed how to subclass a window just using Win32 calls.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          E 1 Reply Last reply
          0
          • S Stuart Dootson

            Yes - the first link[^] in my message showed how to subclass a window just using Win32 calls.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            E Offline
            E Offline
            EvScott
            wrote on last edited by
            #5

            I'm not using a dialog box, I'm using CreateWindow to draw my controls. It appears that the code that you pointed me to is more appropriate for windows interaction with a dialog ctrl: "...The following example shows how to subclass an instance of an edit control in a dialog box..." If I'm not understanding it correctly, then where in the code would I make changes in order for it to work with my application? I have implemented LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) and int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)

            S 1 Reply Last reply
            0
            • E EvScott

              I'm not using a dialog box, I'm using CreateWindow to draw my controls. It appears that the code that you pointed me to is more appropriate for windows interaction with a dialog ctrl: "...The following example shows how to subclass an instance of an edit control in a dialog box..." If I'm not understanding it correctly, then where in the code would I make changes in order for it to work with my application? I have implemented LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) and int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              EvScott wrote:

              I'm not using a dialog box, I'm using CreateWindow to draw my controls.

              That's fine - it works just the same. Here's a code fragment for a small Win32 app I just created - it's just a standard VS2008 Windows app with an edit box created in the standard window. I've sub-classed the edit control to trap the WM_CHAR message and increment the character code (so if you press 'a', the edit control sees 'b'):

              // For storing the edit control's original wndproc
              WNDPROC wpOrigEditProc;

              // The sub-classed edit control's wndproc
              LRESULT APIENTRY EditSubclassProc(
              HWND hwnd,
              UINT uMsg,
              WPARAM wParam,
              LPARAM lParam)
              {
              // Increment the key code for a WM_CHAR message
              if (uMsg==WM_CHAR)
              ++wParam;

              // Pass all messages on to the original wndproc. Obviously, the WM_CHAR one has been tampered with :-)
              return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
              }

              // The main window's wndproc - I've not shown all of it, just the bit that interacts with the edit control
              LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
              {
              int wmId, wmEvent;
              PAINTSTRUCT ps;
              HDC hdc;

              switch (message)
              {
              case WM_CREATE:
              // Create the window
              editWindow = CreateWindow(_T("EDIT"), _T("edit"), WS_CHILD|WS_BORDER, 10, 10, 300, 300, hWnd, 0, hInst, 0);
              // Sub-class it
              wpOrigEditProc = (WNDPROC) SetWindowLong(editWindow, GWL_WNDPROC, (LONG) EditSubclassProc);
              // Show it
              ShowWindow(editWindow, SW_SHOWNORMAL);
              break;

              // the rest of the WndProc follows here

              Simple, eh?

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              E 1 Reply Last reply
              0
              • S Stuart Dootson

                EvScott wrote:

                I'm not using a dialog box, I'm using CreateWindow to draw my controls.

                That's fine - it works just the same. Here's a code fragment for a small Win32 app I just created - it's just a standard VS2008 Windows app with an edit box created in the standard window. I've sub-classed the edit control to trap the WM_CHAR message and increment the character code (so if you press 'a', the edit control sees 'b'):

                // For storing the edit control's original wndproc
                WNDPROC wpOrigEditProc;

                // The sub-classed edit control's wndproc
                LRESULT APIENTRY EditSubclassProc(
                HWND hwnd,
                UINT uMsg,
                WPARAM wParam,
                LPARAM lParam)
                {
                // Increment the key code for a WM_CHAR message
                if (uMsg==WM_CHAR)
                ++wParam;

                // Pass all messages on to the original wndproc. Obviously, the WM_CHAR one has been tampered with :-)
                return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
                }

                // The main window's wndproc - I've not shown all of it, just the bit that interacts with the edit control
                LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
                {
                int wmId, wmEvent;
                PAINTSTRUCT ps;
                HDC hdc;

                switch (message)
                {
                case WM_CREATE:
                // Create the window
                editWindow = CreateWindow(_T("EDIT"), _T("edit"), WS_CHILD|WS_BORDER, 10, 10, 300, 300, hWnd, 0, hInst, 0);
                // Sub-class it
                wpOrigEditProc = (WNDPROC) SetWindowLong(editWindow, GWL_WNDPROC, (LONG) EditSubclassProc);
                // Show it
                ShowWindow(editWindow, SW_SHOWNORMAL);
                break;

                // the rest of the WndProc follows here

                Simple, eh?

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                E Offline
                E Offline
                EvScott
                wrote on last edited by
                #7

                Thanks for your time and patience Stuart. I'm having problems with the editWindow = CreateWindow("EDIT", "edit", WS_CHILD|WS_BORDER, 10, 10, 300, 300, hWnd, 0, hInstance, 0); It doesn't like the hInstance parameter as I have created my Edit and command button windows in the WINAPI WinMain and I use its hInstance parameter and not the LRESULT CALLBACK WndProc Is there any way round that? Am I doing something obviously wrong?

                S 1 Reply Last reply
                0
                • E EvScott

                  Thanks for your time and patience Stuart. I'm having problems with the editWindow = CreateWindow("EDIT", "edit", WS_CHILD|WS_BORDER, 10, 10, 300, 300, hWnd, 0, hInstance, 0); It doesn't like the hInstance parameter as I have created my Edit and command button windows in the WINAPI WinMain and I use its hInstance parameter and not the LRESULT CALLBACK WndProc Is there any way round that? Am I doing something obviously wrong?

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #8

                  Just tried that (creating the edit window outside the WndProc - just after the main window's CreateWindow, but before a ShowWindow call) - I had to add WS_VISIBLE to the style parameter when I moved the edit window creation out of the WndProc. So, change WS_CHILD|WS_BORDER to WS_CHILD|WS_BORDER|WS_VISIBLE.

                  EvScott wrote:

                  It doesn't like the hInstance parameter as I have created my Edit and command button windows in the WinMain

                  Can you define "doesn't like" a little more exactly?

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  E 1 Reply Last reply
                  0
                  • S Stuart Dootson

                    Just tried that (creating the edit window outside the WndProc - just after the main window's CreateWindow, but before a ShowWindow call) - I had to add WS_VISIBLE to the style parameter when I moved the edit window creation out of the WndProc. So, change WS_CHILD|WS_BORDER to WS_CHILD|WS_BORDER|WS_VISIBLE.

                    EvScott wrote:

                    It doesn't like the hInstance parameter as I have created my Edit and command button windows in the WinMain

                    Can you define "doesn't like" a little more exactly?

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    E Offline
                    E Offline
                    EvScott
                    wrote on last edited by
                    #9

                    Thanks a million Stuart - you're the man. It worked a treat. :-D

                    E 1 Reply Last reply
                    0
                    • E EvScott

                      Thanks a million Stuart - you're the man. It worked a treat. :-D

                      E Offline
                      E Offline
                      EvScott
                      wrote on last edited by
                      #10

                      Actually, I've just run it with my code, and the system crashes with an error showing up at return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam); When I debug the code, I notice that hwnd has a valid handle but claims to be unused. Details as follows: hwnd 0x000f070e {unused = ???}

                      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