tags:

views:

201

answers:

3

I want to know the value of virtual key pressed when a child window(like 'edit' or 'button') has focus.
How to do that?

+1  A: 

Well one way is to use

WNDPROC g_OldProc;

LRESULT CALLBACK MyEditWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    if ( uMsg == WM_KEYDOWN )
    {
         // Handle key down.
    }
    return g_OldProc( hwnd, uMsg, wParam, lParam );
}

then at some opportune moment

g_OldProc = (WNDPROC)GetWindowLongPtr( hEdit, GWLP_WNDPROC );
SetWindowLongPtr( hEdit, GWLP_WNDPROC, (LONG_PTR)MyEditWindowProc );

This will replace the window procedure of the hEdit control with your own window procedure that, in turn, calls the original window procedure.

Goz
I'm assuming that SetClassLongPtr does not work because I use it on 'base' classes like 'button'.
hash
I would assume SetClassLongPtr only affects windows created after the call. I don't KNOW on that one though. I've never had call to use it.
Goz
You are right again, thank you much.
hash
you're welcome :)
Goz
A: 

You could catch them at the level of the message loop (before calling DispatchMessage). Nasty but will work.

tyranid
A: 

You could use the GetKeyState Win32 API from within a WM_SETFOCUS handler.

Brian R. Bondy