Trapping WM_KEYDOWN and WM_KEYUP on entering text in edit control
-
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
-
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
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
-
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
-
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
-
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
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)
-
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)
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
-
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
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?
-
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?
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
-
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
-
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 = ???}