Solved

Capture keypresses from all child windows in mfc?

Posted on 2011-02-26
7
1,420 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
Better understanding on C++ Class serialization and formats 9 106
Unable to start eclipse ? 17 176
Create a path if not exists 7 128
Mobile apps - web/native or hybrid? 1 121
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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