views:

48

answers:

3

In my main application Form I have a number of menu and toolbar button functions to which I have assigned shortcut keys such as F10, F11 and F12. The shortcuts work well except for instances where certain controls have focus, such as a 3rd-party grid control. In that case the F11 or other shortcuts are handled by that control and swallowed... they never make it to my event handler of the main UI.

I don't really want to, and can't with the 3rd party controls, modify each control (there are dozens) to pass on my F* keys. I thought of hooking the app's message pump but that seems like a severe solution. Is there a better, cleverer way to handle this situation?

(This is a .NET WinForms MDI (2.0) application which hosts many kinds of home-spun as well as 3rd party components.)

A: 

Have your tried to catch the key presses at the MDI Child form level? Then, you could pass on relevant F* key presses to the MDI Parent in one place.

HardCode
Thought of that (and my child forms all inherit from a base form) but many of the child forms host the offending controls which are capturing the input.
Paul Sasik
+1  A: 

What about setting the Form's KeyPreview property to True? You should be able to intercept the keys that you want or let them pass through.

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

If that doesn't work, you may be able to override WndProc and handle the messages directly, but that would take more effort.

Chris Dunaway
I do have KeyPreview turned on for handling arrow keys already. I already checked this, and for some reason F* keys are not trapped like other keys. Do you have any useful WndProc or hooking links handy? i think that might be my only option at this point.
Paul Sasik
OK. I tried WndProc too but the child control is still trapping the key presses. E.g. My WndProc in the main app form never gets the WM_KEYUP when focus is set in a child, 3rd party grid.
Paul Sasik
A: 

Here is what I tried:

  • Overrides Sub WndProc
  • Overrides Function ProcessKeyPreview
  • IMessageFilter.PreFilterMessage

None of these worked although either one should have. It's my suspicion that the offending child control does something similar and is not playing nice. It's an old Win32 OCX and might actually be doing something really nasty such as hooking into the messages at the OS level. Not sure.

In any case, the workaround was to use Alt + SomeOtherKey. For whatever reason, using Alt + D instead of F11 allows the message handling and focus/view change to take place properly.

This solution is a hack around a 3rd-party control and a genuine black-box issue. I wish I knew exactly what was going on but I don't though I suspect that the 3rd party control has its own hooks into the message pump and is intercepting the F-keys.

Paul Sasik