title: Why are KeyUp and KeyDown events getting slower?
body: <p>I am using the <code>KeyUp</code> and <code>KeyDown</code> events on a WPF Window.</p>
  
  <p>I also have a lot of calls in the CompositionTarget_Rendering event of this window that creates UIElements and animate them. This is done by the engine class below:</p>
  
  <pre><code>int _Fps;
  Stopwatch sw = new Stopwatch();
  
  void CompositionTarget_Rendering_Stats(object sender, EventArgs e)
  {
      _Fps++;
      var ms = sw.ElapsedMilliseconds;
      sw.Restart();
      engine.Update(ms / 1000f);
      timeFrames.Add(ms);
  }
  </code></pre>
  
  <p>I have noticed that the more of elements I have, the slower get <code>KeyUp</code> and <code>KeyDown</code> events.</p>
  
  <p>When I have around 1000 UIelements in the main Window, <strong>the code of both control_KeyDown and control_KeyUp is executed around half a second after I hit or released a key</strong>.</p>
  
  <p>This would not surprise me if the animations inside the Window were laggy too, but it is not the case.</p>
  
  <ul>
  <li>The framerate is at around 55 fps</li>
  <li>The animation remains smooth</li>
  <li>The calculations in the <code>CompositionTarget_Rendering</code> event does not last more than 20 milliseconds.</li>
  </ul>
  
  <p>It seems that only the keyboard event polling is suffering from heavy conditions.</p>
  
  <p>My question are:</p>
  
  <ul>
  <li>What is the magic behind Keyboard handling in WPF: why is it getting laggy under heavy condition while not the rendering process?</li>
  <li>How can I handle better keyboards input and avoid this ?</li>
  </ul>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I wrote a sample inspired by Andy comment. You can copy paste it in the main Windows of a fresh WPF application. It changes the color of the window as a key is down or up, and it fills as many textboxes as possible in the CompositionTarget_Rendering event.</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      WrapPanel root2;
  
      public MainWindow()
      {
          InitializeComponent();
          root2 = new WrapPanel();
          root2.Margin = new Thickness(10);
          this.Content = root2;
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          for (var i = 0; i &lt; 2000; i++)
              root2.Children.Add(new TextBlock() { Background = Brushes.Yellow });
  
          this.KeyDown += MainWindow_KeyDown;
          this.KeyUp += MainWindow_KeyUp;
          CompositionTarget.Rendering += CompositionTarget_Rendering;
      }
  
      void MainWindow_KeyUp(object sender, KeyEventArgs e)
      {
          this.Background = Brushes.Red;
      }
  
      void MainWindow_KeyDown(object sender, KeyEventArgs e)
      {
          this.Background = Brushes.Green;
      }
  
      void CompositionTarget_Rendering(object sender, EventArgs e)
      {
          foreach (var child in root2.Children)
              ((TextBlock)child).Text = DateTime.Now.Millisecond.ToString();
      }
  }
  </code></pre>
  
  <p>Depending on your machine performances, change the 2000 to increase the number of textboxes. At a certain amount, there is more CompositionTarget_Rendering triggers than KeyUp or KeyDown. It is quite visible when a key is pressed to see the color change of the border: the key is down, then the Composition_Rendering is triggered several time, then the keydown event is triggered and the border turns to red.</p>