Solved

ListView VK_F5 WM_NOTIFY

Posted on 2007-03-27
5
1,090 Views
Last Modified: 2013-12-04
My app is a win32 app written in C++. I have a SysListView32 in a window and I need to get notification when the user presses the F5 key. With Spy++ I can see that the ListView window is seeing the WM_KEYDOWN for VK_F5, but this information is not being sent back to the partent window handler (ie MY code). I've tried to look for it in a WM_NOTIFY message, but the ListView does not seem to send that to me for an F5 key press. Any ideas?
0
Comment
Question by:dddogget
  • 3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18801954
Brute force: Hook into the SysListView32's message processing using a local "WH_KEYBOARD" or "WH_GETMESSAGE" hook, e.g.

HHOOK       g_hhk   =   NULL;
HWND        g_hwndNotify = NULL;

LRESULT CALLBACK HookProc   (   int     nCode,  // hook code
                                WPARAM  wParam, // removal flag
                                LPARAM  lParam  // address of structure with message
                            )
{
    PMSG    pmsg    =   ( PMSG) lParam;

    if  (   0   >   nCode   ||  PM_NOREMOVE ==  wParam)
        {
            return  (   CallNextHookEx  (   g_hhk,
                                            nCode,
                                            wParam,
                                            lParam
                                        )
                    );
        }

    if  (       WM_KEYDOWN  ==  pmsg->message
            ||  WM_KEYUP    ==  pmsg->message  
        )
        {

            g_iMap  =   g_map.find  (   pmsg->wParam);

            if  (   VK_F5 == pmsg->wParam) PostMessage ( g_hwndNotify, /* other parameters here */);
        }

    return  (   CallNextHookEx  (   g_hhk,
                                    nCode,
                                    wParam,
                                    lParam
                                )
            );
}

LONG InitHook   ( HWND hwndNotify)
{


    if  (   g_hhk)  return  (   ERROR_ALREADY_EXISTS);

    g_hwndNotify = hwndNotify;

    g_hhk   =   SetWindowsHookEx    (   WH_GETMESSAGE,
                                        ( HOOKPROC) HookProc,
                                        0,
                                        0
                                    );
    return 0;
}

For more on hooks, see http://msdn2.microsoft.com/en-us/library/ms997537 ("Win32 Hooks")
0
 
LVL 86

Expert Comment

by:jkr
ID: 18801958
Ooops, please ignore the line 'g_iMap  =   g_map.find  (   pmsg->wParam);'
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 18802170
Hn, actually, you should filter for the SysListView32's HWND. Then that should be

HOOK       g_hhk   =   NULL;
HWND        g_hwndNotify = NULL;
HWND        g_hwndToWatch = NULL;

LRESULT CALLBACK HookProc   (   int     nCode,  // hook code
                                WPARAM  wParam, // removal flag
                                LPARAM  lParam  // address of structure with message
                            )
{
    PMSG    pmsg    =   ( PMSG) lParam;

    if  (   0   >   nCode   ||  PM_NOREMOVE ==  wParam || pmsghWnd != g_hwndToWatch)
        {
            return  (   CallNextHookEx  (   g_hhk,
                                            nCode,
                                            wParam,
                                            lParam
                                        )
                    );
        }

    if  (       WM_KEYDOWN  ==  pmsg->message
            ||  WM_KEYUP    ==  pmsg->message  
        )
        {

            if  (   VK_F5 == pmsg->wParam) PostMessage ( g_hwndNotify, /* other parameters here */);
        }

    return  (   CallNextHookEx  (   g_hhk,
                                    nCode,
                                    wParam,
                                    lParam
                                )
            );
}

LONG InitHook   ( HWND hwndNotify, HWND hwndToWatch)
{


    if  (   g_hhk)  return  (   ERROR_ALREADY_EXISTS);

    g_hwndNotify = hwndNotify;
    g_hwndToWatch = hwndToWatch

    g_hhk   =   SetWindowsHookEx    (   WH_GETMESSAGE,
                                        ( HOOKPROC) HookProc,
                                        g_hThisDll,
                                        0
                                    );
    return 0;
}
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 total points
ID: 18806083
>>>>> Brute force: Hook into the SysListView32's message

If using MFC it's much easier to overload your dialog or view class PreTranslateMessage member function. Here you'll get all keyboard message before they are dispatched to the controls.

If not using MFC you might change the message loop of your parent's window. Here you should have something like

    while (GetMessage(..))
    {
          TranslateMessage(...);
          DispatchMessage(...);
    }

There you may filter any keyboard message before it is transferred to the controls.

Regards, Alex
0
 

Author Comment

by:dddogget
ID: 18845735
I appreciate both solutions. While both look like they should work, I implented teh one from "itsmeandnobodyelse" because of it's simplicity. Generally, 10 lines of code is better than 30.

Much thanks to both!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 55
C++ error cannot convert from std::string to const char * 6 122
WPF issue with Trigger 2 91
Event ID: 1202 / Source: SceCli 6 83
We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now