title: How can i check constantly the keyboard state? Instead of using the event handler
body: <p>I'm creating a Galaga type game and I am focusing on the ship's movement. I have this code: </p>
  
  <pre><code>public partial class MainWindow : Window
  {        
      public MainWindow()
      {
          InitializeComponent();
      }      
  
      private void Window_KeyDown(object sender, KeyEventArgs e)
      {
          if (e.Key == Key.Left)
          {
              moveLeft();
          }
          else if (e.Key == Key.Right)
          {
              moveRight();
          }
          else if (e.Key == Key.Space)
          {
              bullet();
          }
      }
  
      private async void bullet()
      {
          Line bullet = new Line();
          bullet.X1 = Canvas.GetLeft(testMovement) + (testMovement.Width / 2);
          bullet.Y1 = Canvas.GetTop(testMovement);
  
          bullet.X2 = bullet.X1;
          bullet.Y2 = bullet.Y1 + 3;
  
          bullet.Stroke = new SolidColorBrush(Color.FromRgb(255,255,255));
          bullet.StrokeThickness = 3;
  
          horizontalMove.Children.Add(bullet);
  
          for (int i = 0; i &lt; 50; i++)
          {
              await Task.Delay(20);
              shoot(bullet);
          }
  
          horizontalMove.Children.Remove(bullet);
      }
  
      private void shoot(Line bullet) 
      {        
              bullet.Y2 = bullet.Y1;
              bullet.Y1 -= 5;  
      }
  
      int x = 0;
      private void moveRight() 
      {
          if (x&gt;=0 &amp;&amp; x &lt;= 370)
          {
              if(x == 370)
              {
                  x -= 10;
              }
              Canvas.SetLeft(testMovement, x += 10);
          }
          else
          {
              if (x &lt; 0)
                  x = 0;
              else 
                  x = 370;
          }
          label.Content = x ;
      }
  
      private void moveLeft() 
      {
          if (x&gt;=0 &amp;&amp; x &lt;= 370)
          {
              if (x == 0) 
              {
                  x += 10;
              }
              Canvas.SetLeft(testMovement, x -= 10);
          }
          else
          {
              if (x &lt; 0)
                  x = 0;
              else
                  x = 370;
          }
          label.Content = x;
      }
  
      private void Window_KeyUp(object sender, KeyEventArgs e) {}
  
  }
  </code></pre>
  
  <p>The problem with this code is that when I change directions, it has a certain delay and the animation is not so smooth. </p>
  
  <p>So, how should I proceed if I want to constantly check for the keyboard's state (for instance, whether left, right or space is clicked) without using the event handler?</p>