Solved

ListView VK_F5 WM_NOTIFY

Posted on 2007-03-27
5
1,129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
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 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.

635 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