Solved

ListView VK_F5 WM_NOTIFY

Posted on 2007-03-27
5
1,102 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

803 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