Link to home
Start Free TrialLog in
Avatar of chrispauljarram
chrispauljarram

asked on

Capture keypresses from all child windows in mfc?

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
Avatar of jkr
jkr
Flag of Germany image

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

Avatar of chrispauljarram
chrispauljarram

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
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.
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
Good that you found it yourself, I was still in bed at the time you asked ;o)