Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Capture keypresses from all child windows in mfc?

Posted on 2011-02-26
7
Medium Priority
?
1,494 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
[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
  • 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 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

722 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