Originally Posted by
zypronix
Hi,
I'm trying to get the key presses sent to an edit control. I tried getting them through the WM_KEYDOWN/UP messages, but those messages aren't sent to the windows procedure callback I expected them to be sent to.
I'm trying to intercept the key presses because I want to print their "VK_" identifiers in the edit control, but only when the user has selected the box.
I Googled for help, but couldn't find anything aside from something about hooks, which I couldn't understand. Is there a better, easier way to do what I'm attempting to do without using hooks?
One alternative to using hooks would be to superclass the "edit" control wndclass, like this:
Code:
WNDCLASS wndclass;
WNDPROC OldWndProc;
HINSTANCE hInst;
/* ... */
hInst = GetModuleHandle(0);
GetClassInfo(hInst, TEXT("edit"), &wndclass);
OldWndProc = wndclass.lpfnWndProc;
wndclass.lpfnWndProc = MyEditBoxProc;
wndclass.lpszClassName = TEXT("myedit");
wndclass.hInstance = hInst;
RegisterClass(&wndclass);
GetClassInfo() fills the supplied WNDCLASS structure with the edit control's default stuff, including its pointer to its default window procedure. You're going to switch its default window procedure function out for your own, but before you do you save the pointer to the default function so you can still call it to handle all the other stuff you expect an edit control to magically handle for you. You also give your superclass a new name and assign it some other stuff, then register it just like any other WNDCLASS structure.
Code:
LRESULT CALLBACK MyEditBoxProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_KEYDOWN:
/* Catch and process keypresses here */
return 0;
default:
break;
}
return CallWindowProc(OldWndProc, hwnd, msg, wparam, lparam);
}
To create an edit control which uses MyEditBoxProc, you use TEXT("myedit") for the lpClassName in your CreateWindow call instead of TEXT("edit").