Solved

Capture keypresses from all child windows in mfc?

Posted on 2011-02-26
7
1,398 Views
Last Modified: 2012-05-11
Hi,

This should be a really simple one but for some reason I can't see why it isn't working.  I have a PretranslateMsg override in my CFrameWnd derived top level window.  This window has a number of child dialogs, each of which have child dialogs and / or controls themselves.  Although from what I can see the parent chain is correct when creating these windows (ultimately they all lead back up to the top level window), for some reason events suchs as keypresses are not being reported if for example a control in a nested child dialog (such as a combo box) has focus.  I need global key reponse in my app for shortcut keys, but without having to bugger about with Windows Hooks / Hotkeys or anything silly like that when really this should be MFC 101.  Is there a property I need to set on my nested child controls to force them to propogate any keypresses back up the chain, or have I missed something more fundamental?

Sorry to be so stupid :-/

Thanks,
Chris
0
Comment
Question by:chrispauljarram
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 34988166
Hmm, yet a hook (limited to your process, a global one would be crazy) would indeed be the easiest solution - just like
HHOOK g_hhk;

LRESULT CALLBACK KeyboardProc(
    int code,
    WPARAM wParam,
    LPARAM lParam
)
{
  if (0 > code) CallNextHookEx(g_hhk,code,wParam,lParam);

   // ...
   // extract virtual key code from 'wParam'

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

// your code

g_hhk = SetWindowsHookEx(WH_KEYBOARD,KeyboardProc,NULL,GetCurrentThreadId());

Open in new window

0
 

Author Comment

by:chrispauljarram
ID: 34988257
Hi again jkr,

I agree it would be simple enough to do this, I guess I just wanted to know what the 'correct' mechanism is to use here as Windows Hooks are primarily intended for system-wide events and surely this must be an everyday problem... it's also a learning experience for me as I'd like to undestand why these key events aren't being propogated to parents, are there any window properties that can be changed to force this to happen?

It does only seem to be the case for child dialogs, their messages are not propogated to their parents.  Sounds a little like the thread here which I've just stumbled across where someone proposed a solution (last post)...

http://www.codeguru.com/forum/archive/index.php/t-83000.html

Might give this a go a bit later, is there any other reason you can think of this might not be working?

Thanks again,
Chris
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 34988917
Well, parent windows are only informed about creation/destruction of child windows (via WM_PARENTNOTIFY), but not about keypress events, these are handled within the children which have their own WndProc - so, other that alter teh child's code to propagate keypress events to their parents, a hook would be the cleanest solution.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:chrispauljarram
ID: 34989047
OK cool, I've gone ahead and done that - I did try the other method but it didn't work.

Thanks again for the advice, sorry it was a bit of a mediocre question!
0
 

Author Comment

by:chrispauljarram
ID: 34991159
Hi jkr

Hmm, think I closed this question a little too hastily - I knew there was a reason why I thought using non-conventional means to do common tasks was probably a bad idea.

I have things such as menu accelerators (e.g. Ctrl-C to close) which were suffering the same problems when child dialogs had focus.  So, all fine and well I moved these key captures to my Windows Hook, the problem now is say I hit Ctrl-C and a dialog pops up to offer to save changes (if they've been made), hitting Ctrl-C again will being this dialog up again so now I have 2.  I don't want my main app window to recieve these key events if any modal message boxes or dilaogs are open on top of it, as this could start causing untold numbers of problems.

I'm sure you agree It's never good to have to write additional code to undo the side effects of other code, can you think of an elegant way around this?   I guess essentially I need to be able to check in my KeyboardProc if the Main Application window (or any of it's nested child windows) has focus (ideally without using hacky approaches like EnumChildWindows that could just lead to more problems).

Thanks,
Chris.
0
 

Author Comment

by:chrispauljarram
ID: 34991191
Never mind, using IsWindowEnabled() does the trick it seems.

There is more info on this problem here (and some extra help in the comments section):-
http://www.codeproject.com/KB/dialog/pretransdialog01.aspx

Thanks again,
Chris
0
 
LVL 86

Expert Comment

by:jkr
ID: 34992567
Good that you found it yourself, I was still in bed at the time you asked ;o)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX TCameraComponent Problem 2 82
designing in object programming 12 94
VIDEO DATASOURCE Control Resize issue 2 30
C++ Code Issue 4 25
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

856 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