Murugan.com
Murugan Andezuthu Dharmaratnam

  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  



VC++ Win32 Trapping Edit Enter Key Press WM_KEYDOWN VK_RETURN

Home   VC++   VC++ Win32 Trapping Edit Enter Key Press WM_KEYDOWN VK_RETURN         



I have created a child window of class EDIT . When I click on enter I am not able to trap it. How do I Trap the enter key . I would appreciate some help in being able to 'trap' a keydown and keyup event when text is entered in an edit control.

Solution

You can subclass  the edit control, to process the Windows messages you want to, before passing them on to the edit control.

Here's a some code for a Win32 app its an app with an edit box created in the standard window. I've sub-classed the edit control to trap the WM_KEYDOWN   and check if enter key is pressed using VK_RETURN, 


WNDPROC wpOrigEditProc;
// The sub-classed edit control's wndproc
LRESULT APIENTRY EditSubclassProc(
                                  HWND hwnd, 
                                  UINT uMsg, 
                                  WPARAM wParam, 
                                  LPARAM lParam) 
{ 
	switch(uMsg)
	{
		case VK_RETURN:
			MsgBox("Return Key Pressed");
			break;
		case WM_KEYDOWN: 
			if (wParam == VK_RETURN)
			{
				MsgBox("Enter Key Pressed");
			}
			break;

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

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{

	INITCOMMONCONTROLSEX icex;
    
    // Ensure that the common control DLL is loaded. 
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC  = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&icex); 
    InitCommonControls();	
	RECT rcl; 
	HWND hWnd;
	hInst = hInstance; // Store instance handle in our global variable

	hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);


hWndTB = CreateWindow("EDIT", 
                                "", 
                                WS_CHILDWINDOW|WS_BORDER|ES_AUTOHSCROLL|WS_CHILD|WS_VISIBLE, 
                                x, 
                                y, 
                                width, 
                                height, 
                                hWnd, 
                                NULL, 
                                hInst, 
                                NULL); 

wpOrigEditProc = (WNDPROC) SetWindowLong(hWndTB , GWL_WNDPROC, (LONG) EditSubclassProc); 

   ShowWindow(hWnd, nCmdShow);
   ShowWindow(hWndTB ,nCmdShow);
   UpdateWindow(hWnd);
   UpdateWindow(hWndTB );
   return TRUE;
}






  |  HOME   |  BLOG   |  TWITTER   |  ARTICLES   |  8086  |  C++   |  VC++   |  ASP .NET   |  VB .NET   |  JAVA SCRIPT   |  MS SQL   |  MY   |  VIDEOS   |  DOWNLOADS   |  CONTACT ME   |  

Copyright 2009 @ Murugan Andezuthu Dharmaratnam