title: XNA, how do I know what key is being pressed?
body: <p>SKIP TO EDIT 3:
  I plan to make use of this that I found online. However, I can't figure out how the proper variable that works for its parameter of <code>Keys key</code>. I can't figure out what type of variable I have to send to the procedure. I tried sending my <code>Keyboard kbState = Keyboard.GetState()</code> but it didn't work. I tried sending <code>kbState.GetPressedKeys()</code> but it said it wouldn't work. How do I do it?</p>
  
  <pre><code>public static string ConvertKeyToChar(Keys key, bool shift) 
  { 
      switch (key) 
      { 
          case Keys.Space: return " "; 
  
          // Escape Sequences 
          case Keys.Enter: return "\n";     // Create a new line 
          case Keys.Tab: return "\t";       // Tab to the right 
  
          // D-Numerics (strip above the alphabet) 
          case Keys.D0: return shift ? ")" : "0"; 
          case Keys.D1: return shift ? "!" : "1"; 
          case Keys.D2: return shift ? "@" : "2"; 
          case Keys.D3: return shift ? "#" : "3"; 
          case Keys.D4: return shift ? "$" : "4"; 
          case Keys.D5: return shift ? "%" : "5"; 
          case Keys.D6: return shift ? "^" : "6"; 
          case Keys.D7: return shift ? "&amp;" : "7"; 
          case Keys.D8: return shift ? "*" : "8"; 
          case Keys.D9: return shift ? "(" : "9"; 
  
          // Numpad 
          case Keys.NumPad0: return "0"; 
          case Keys.NumPad1: return "1"; 
          case Keys.NumPad2: return "2"; 
          case Keys.NumPad3: return "3"; 
          case Keys.NumPad4: return "4"; 
          case Keys.NumPad5: return "5"; 
          case Keys.NumPad6: return "6"; 
          case Keys.NumPad7: return "7"; 
          case Keys.NumPad8: return "8"; 
          case Keys.NumPad9: return "9"; 
          case Keys.Add: return "+"; 
          case Keys.Subtract: return "-"; 
          case Keys.Multiply: return "*"; 
          case Keys.Divide: return "/"; 
          case Keys.Decimal: return "."; 
  
          // Alphabet 
          case Keys.A: return shift ? "A" : "a"; 
          case Keys.B: return shift ? "B" : "b"; 
          case Keys.C: return shift ? "C" : "c"; 
          case Keys.D: return shift ? "D" : "d"; 
          case Keys.E: return shift ? "E" : "e"; 
          case Keys.F: return shift ? "F" : "f"; 
          case Keys.G: return shift ? "G" : "g"; 
          case Keys.H: return shift ? "H" : "h"; 
          case Keys.I: return shift ? "I" : "i"; 
          case Keys.J: return shift ? "J" : "j"; 
          case Keys.K: return shift ? "K" : "k"; 
          case Keys.L: return shift ? "L" : "l"; 
          case Keys.M: return shift ? "M" : "m"; 
          case Keys.N: return shift ? "N" : "n"; 
          case Keys.O: return shift ? "O" : "o"; 
          case Keys.P: return shift ? "P" : "p"; 
          case Keys.Q: return shift ? "Q" : "q"; 
          case Keys.R: return shift ? "R" : "r"; 
          case Keys.S: return shift ? "S" : "s"; 
          case Keys.T: return shift ? "T" : "t"; 
          case Keys.U: return shift ? "U" : "u"; 
          case Keys.V: return shift ? "V" : "v"; 
          case Keys.W: return shift ? "W" : "w"; 
          case Keys.X: return shift ? "X" : "x"; 
          case Keys.Y: return shift ? "Y" : "y"; 
          case Keys.Z: return shift ? "Z" : "z"; 
  
          // Oem 
          case Keys.OemOpenBrackets: return shift ? "{" : "["; 
          case Keys.OemCloseBrackets: return shift ? "}" : "]"; 
          case Keys.OemComma: return shift ? "&lt;" : ","; 
          case Keys.OemPeriod: return shift ? "&gt;" : "."; 
          case Keys.OemMinus: return shift ? "_" : "-"; 
          case Keys.OemPlus: return shift ? "+" : "="; 
          case Keys.OemQuestion: return shift ? "?" : "/"; 
          case Keys.OemSemicolon: return shift ? ":" : ";"; 
          case Keys.OemQuotes: return shift ? "\"" : "'";
          case Keys.OemPipe: return shift ? "|" : "\\";
          case Keys.OemTilde: return shift ? "~" : "`"; 
      } 
  
      return string.Empty; 
  } 
  </code></pre>
  
  <p>EDIT:
  So I tried this in the update procedure of XNA.</p>
  
  <pre><code>var newkbState = Keyboard.GetState();
  Keys[] pressedKeys = newkbState.GetPressedKeys();
  newInput = null;
  
  if (newkbState.IsKeyDown(Keys.LeftShift) || newkbState.IsKeyDown(Keys.RightShift))
  {
      shiftNew = true;
  }
  else
  {
      shiftNew = false;
  }
  
  for (int count = 0; count &lt; pressedKeys.Length; count++)
  {
      newInput += ConvertKeyToChar(pressedKeys[count], shiftNew) ;
  }
  
  if (newInput != oldInput)
  {
      text2 += newInput;
  }
  oldInput = newInput;
  </code></pre>
  
  <p>Now my problem is that, if I hold S, and pressed O, it'll type SOSS. Is there anyway i can stop this from happening?</p>
  
  <p>EDIT2:
                 var newkbState = Keyboard.GetState();
                 newkbState = GetKeysPressedBetween(oldkbState, newkbState);
                 Keys[] pressedKeys;
                 pressedKeys = newkbState.GetPressedKeys();</p>
  
  <pre><code>            if (newkbState.IsKeyDown(Keys.LeftShift) ||       newkbState.IsKeyDown(Keys.RightShift))
              {
                  shiftNew = true;
              }
              else
              {
                  shiftNew = false;
              }
  
              newInput = "";
  
              for (int count = 0; count &lt; pressedKeys.Length; count++)
              {
                  newInput += ConvertKeyToChar(pressedKeys[count], shiftNew);
  
              }
  
             if ((newInput != oldInput) &amp;&amp; (newkbState != oldkbState))
              {
              text2 += newInput;
              }
             oldInput = newInput;
              oldkbState = newkbState;
  </code></pre>
  
  <p>Alright so, I tried the answerer's code and now I'm having problems. What's happening is:
  1. Just tapping the letter "a" once outputs 3 a's.
  2. When two buttons are pressed, a and b, the first problem happens and abababababab just keeps repeating as i hold onto it. If I fix the first problem, I'm not sure if the second would fix. </p>