views:

88

answers:

1

Hi I have an application built in wpf.

At the base level I have a usercontrol and on that usercontrol are many components.

I want to be able to capture a key press event on my usercontrol even if the focus is on one of the children. Using PreviewKeyUp doesn't seem to do the trick.

Thanks, Matt.

A: 

Maybe I understood your question not fully, but if you don't set the keydown event to handled, it should go up the hierarchy.

public UserControl1()
          {
               InitializeComponent();
               this.KeyDown += new KeyEventHandler(UserControl1_KeyDown);
               textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown);
          }

          void textBox1_KeyDown(object sender, KeyEventArgs e)
          {
               Console.WriteLine("txt keydown: "+e.Key.ToString());
          }

          void UserControl1_KeyDown(object sender, KeyEventArgs e)
          {
               Console.WriteLine(e.Key.ToString());
          }

output:

txt keydown: Y
Y
Sdry