Capture KeyUp event on form when child control has focus

Posted by Jon B on Stack Overflow See other posts from Stack Overflow or by Jon B
Published on 2009-06-19T14:36:40Z Indexed on 2010/04/22 11:13 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

I need to capture the KeyUp event in my form (to toggle a "full screen mode"). Here's what I'm doing:

protected override void OnKeyUp(KeyEventArgs e)
{
    base.OnKeyUp(e);

    if (e.KeyCode == Keys.F12) this.ToggleFullScreen();
}

private void ToggleFullScreen()
{
    // Snazzy code goes here           
}

This works fine, unless a control on the form has focus. In that case, I don't get the event at all (also tried OnKeyDown - no luck there either).

I could handle the KeyUp event from the child control, but the controls on the form are generated dynamically, and there may be many of them - each having many children of their own.

Is there any way to do this without generating event handlers for every control on the screen (which I certainly could do with a recursive function)?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET