Solved

ListView VK_F5 WM_NOTIFY

Posted on 2007-03-27
5
1,082 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
Comment Utility
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
Comment Utility
Ooops, please ignore the line 'g_iMap  =   g_map.find  (   pmsg->wParam);'
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
Comment Utility
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
Comment Utility
>>>>> 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

15 Experts available now in Live!

Get 1:1 Help Now