Solved

Capture keypresses from all child windows in mfc?

Posted on 2011-02-26
7
1,365 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now