title: Detecting keys pressed without events in UWP
body: <p>I am writing a simple 2D game engine for UWP. I have a code snippet taken from XNA, checking for if a certain key is pressed (see below), and I'm trying to do something similar.</p>
  
  <p>Indeed, in my game engine I'd rather not use the dedicated event handlers (KeyDown etc), but instead simply check for pressed keys in my game loop - if this is possible to do. One reason for this is that I can then control for example how often a bullet fires if the key is continuously pressed (using a timer and mod). It would also let the keys be independent of each other, such as up and right arrow key gives diagonal movement for player 1, while another player (using the same keyboard) uses keys A and W to move his/her character in another direction, and they both occasionally fire their laser guns. Finally, I have always considered using the event handlers working separately from my the rest of my game code, resulting in lack of control. This is especially true for the KeyPress event, I think.</p>
  
  <p>The code I have found looks like this:</p>
  
  <pre><code> public void DrawHelp()
          {
              if (Keyboard.GetState().IsKeyDown(Keys.Tab))
              {
  </code></pre>
  
  <p>..and this is what I'm trying to use. However, the Keyboard class words don't come up in Intellisense for me (though the Windows.UI.Xaml.Input; using is attached by default).</p>
  
  <p>What I'm thinking of is basically a series of if statements (the code is wrong - it's just to show what I want):</p>
  
  <pre><code>    int counter = 0;
  
      // Raised every tick while the DispatcherTimer is active.
      private void EachTick(object sender, object e)
      {
          TrackKeyboard();
          CalculateMoves();
          Physics.CollisionDetection();
          DrawGame();
      }
  
      private void TrackKeyboard()
      {
          if (Keyboard.GetState().IsKeyDown(Keys.P)) PauseGame();
  
          if (counter % 10 == 0) //control movements
          {
              if (Keyboard.GetState().IsKeyDown(Keys.Down)) AddSpeed("Player1", 0, -1);
              if (Keyboard.GetState().IsKeyDown(Keys.Left)) AddSpeed("Player1", -1, 0);
              if (Keyboard.GetState().IsKeyDown(Keys.Up)) AddSpeed("Player1", -1, 0);
              if (Keyboard.GetState().IsKeyDown(Keys.Right)) AddSpeed("Player1", 0, 1);
              if (Keyboard.GetState().IsKeyDown(Keys.W)) AddSpeed("Player2", -1, 0);
              if (Keyboard.GetState().IsKeyDown(Keys.D)) AddSpeed("Player2", 0, 1);
              if (Keyboard.GetState().IsKeyDown(Keys.S)) AddSpeed("Player2", -1, 0);
              if (Keyboard.GetState().IsKeyDown(Keys.A)) AddSpeed("Player2", 0, 1);
          }
  
          if (counter % 5 == 0) //fire
          {
              if (Keyboard.GetState().IsKeyDown(Keys.Space)) Fire("Player1");
              if (Keyboard.GetState().IsKeyDown(Keys.X)) Fire("Player2");
          }
      }
  </code></pre>
  
  <p>So, is it possible to write something like this in UWP, and if so: How?</p>
  
  <p>I've done some searching on Bing and Google for this, but there is still very little info on UWP. </p>
  
  <p>I have found a similar question on CodeProject (<a href="https://www.codeproject.com/Questions/1107441/How-do-I-register-events-from-two-players-in-UWP" rel="nofollow noreferrer">https://www.codeproject.com/Questions/1107441/How-do-I-register-events-from-two-players-in-UWP</a>), but the answer is too difficult for me to follow, and in any case, I'd much rather include this code in the game loop, as described above.</p>
  
  <p>(Also please let me know if the route I'm trying to take is stupid for some technical reason that I may not be aware of.)</p>
  
  <p>Thanks so much and Happy New Year!</p>
  
  <p>Petter</p>