title: C# WPF keyboard movement for game
body: <p>I'm trying to make a small game in c# using wpf. I have my rectangle moving around, the problem I am trying to fix is if you press another key well holding the original key, then release one of them it will go in the direction being held. The code I have works but if ran once a key is released there is a delay then the rectangle will continue moving.</p>
  
  <pre><code>    enum Direction { left, right, up, down, none};
      Direction _direction = Direction.none;
      bool _directionIsPressed = false;
      bool _directionIsReleased = false;
      double x = 0;
      double y = 0;
  
      public MainWindow()
      {
          InitializeComponent();
  
  
  
          DispatcherTimer timer = new DispatcherTimer();
          timer.Tick += new EventHandler(MovePlayer);
          timer.Start();
  
          this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
          this.KeyUp += new KeyEventHandler(OnButtonKeyRelease);
  
      }
  
      private void MovePlayer(object sender, EventArgs e)
      {
          switch (_direction)
          {
              case Direction.down:
                  y += .05;
                  Canvas.SetTop(Player, y);
                  break;
              case Direction.up:
                  y -= .05;
                  Canvas.SetTop(Player, y);
                  break;
              case Direction.left:
                  x -= .05;
                  Canvas.SetLeft(Player, x);
                  break;
              case Direction.right:
                  x += .05;
                  Canvas.SetLeft(Player, x);
                  break;
          }
      }
  
      private void OnButtonKeyDown(object sender, KeyEventArgs e)
      {
          switch (e.Key)
          {
              case Key.Down:
                  _direction = Direction.down;
                  _directionIsPressed = true;
                  break;
              case Key.Up:
                  _direction = Direction.up;
                  _directionIsPressed = true;
                  break;
              case Key.Left:
                  _direction = Direction.left;
                  _directionIsPressed = true;
                  break;
              case Key.Right:
                  _direction = Direction.right;
                  _directionIsPressed = true;
                  break;
              default:
                  _direction = Direction.none;
                  break;
          }
      }
  
      //When a key is released it will set the direction to none then 
      //reset the direction but there is about a half second delay in 
      //setting the direction again
      private void OnButtonKeyRelease(Object sender, KeyEventArgs e) 
      {
          _direction = Direction.none;
          this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
      }
  </code></pre>
  
  <p><b>Edit:</b>
  If anyone wants smooth keyboard movement in C# using wpf here is how I fixed it. Thank you Stefan for the help.</p>
  
  <pre><code>        public MainWindow()
      {
          InitializeComponent();
          DispatcherTimer timer = new DispatcherTimer();
          timer.Tick += new EventHandler(MovePlayer);
          timer.Start();
      }
  
      private void MovePlayer(object sender, EventArgs e)
      {
          if(Keyboard.IsKeyDown(Key.Left))
          {
              x -= .05;
              Canvas.SetLeft(Player, x);
          }
          if(Keyboard.IsKeyDown(Key.Up))
          {
              y -= .05;
              Canvas.SetTop(Player, y);
          }
          if(Keyboard.IsKeyDown(Key.Down))
          {
              y += .05;
              Canvas.SetTop(Player, y);
          }
          if(Keyboard.IsKeyDown(Key.Right))
          {
              x += .05;
              Canvas.SetLeft(Player, x);
          }
      }
  </code></pre>