/// <summary> /// Has to be called every Update to ensure correct calling /// </summary> public void Update(GameTime gameTime) { // Shift pressed to lastPressed LastPressedKeys = new List <Keys>(PressedKeys); PressedKeys.Clear(); KeyboardState state = Keyboard.GetState(); PressedKeys.AddRange(state.GetPressedKeys()); // Add simulated keys foreach (Keys key in SimulatedKeys) { if (!PressedKeys.Contains(key)) { PressedKeys.Add(key); } } // Get pressed time for gmkeyboard.string // Remove released keys foreach (Keys key in PressTime.Keys.ToList()) { if (!PressedKeys.Contains(key)) { PressTime.Remove(key); } } // Add newly pressed keys to pressTime foreach (Keys key in PressedKeys) { if (!PressTime.ContainsKey(key)) { PressTime.Add(key, -gameTime.ElapsedGameTime.TotalMilliseconds); } } Dictionary <Keys, double> lastPressTime = new Dictionary <Keys, double>(PressTime); TriggeredKeys.Clear(); // Handle KeyboardString foreach (Keys key in PressTime.Keys.ToList()) { // Don't add key to string if it isn't allowed to if (Settings.AddSimulatedKeysToKeyboardString == false) { if (SimulatedKeys.Contains(key)) { break; } } PressTime[key] += gameTime.ElapsedGameTime.TotalMilliseconds; bool shouldFire = false; // Fire if key is just pressed if (PressTime[key] == 0) { shouldFire = true; } // Check if it should refire because key is hold if (PressTime[key] >= Settings.ReFireDelay) { int maxTime = Settings.ReFireDelay + Settings.ReFireInterval * 20; if (PressTime[key] > maxTime) { PressTime[key] -= Settings.ReFireInterval * 20; lastPressTime[key] -= Settings.ReFireInterval * 20; } for (int t = Settings.ReFireDelay; t < maxTime; t += Settings.ReFireInterval) { if (PressTime[key] > t && lastPressTime[key] < t) { shouldFire = true; } } } if (shouldFire) { TriggeredKeys.Add(key); // s = shift pressed? bool s = Check(Keys.LeftShift) | Check(Keys.RightShift); string keyString = key.ToString(); if (Alphabet.Contains(key)) { s = state.CapsLock ? !s : s; // Invert shift if CapsLock is on String += s ? keyString : keyString.ToLower(); } else { bool nl = state.NumLock; switch (key) { case Keys.Space: String += " "; break; case Keys.Back: if (String.Length > 0) { String = String.Substring(0, String.Length - 1); } break; case Keys.D1: String += s ? "!" : "1"; break; case Keys.D2: String += s ? "@" : "2"; break; case Keys.D3: String += s ? "#" : "3"; break; case Keys.D4: String += s ? "$" : "4"; break; case Keys.D5: String += s ? "%" : "5"; break; case Keys.D6: String += s ? "^" : "6"; break; case Keys.D7: String += s ? "&" : "7"; break; case Keys.D8: String += s ? "*" : "8"; break; case Keys.D9: String += s ? "(" : "9"; break; case Keys.D0: String += s ? ")" : "0"; break; case Keys.OemComma: String += s ? "<" : ","; break; case Keys.OemPeriod: String += s ? ">" : "."; break; case Keys.OemQuestion: String += s ? "?" : "/"; break; case Keys.OemSemicolon: String += s ? ":" : ";"; break; case Keys.OemQuotes: String += s ? "\"" : "'"; break; case Keys.OemPipe: String += s ? "|" : "\\"; break; case Keys.OemMinus: String += s ? "_" : "-"; break; case Keys.OemPlus: String += s ? "+" : "="; break; case Keys.OemOpenBrackets: String += s ? "{" : "["; break; case Keys.OemCloseBrackets: String += s ? "}" : "]"; break; case Keys.OemTilde: String += s ? "~" : "`"; break; case Keys.NumPad0: String += nl ? "0" : ""; break; case Keys.NumPad1: String += nl ? "1" : ""; break; case Keys.NumPad2: String += nl ? "2" : ""; break; case Keys.NumPad3: String += nl ? "3" : ""; break; case Keys.NumPad4: String += nl ? "4" : ""; break; case Keys.NumPad5: String += nl ? "5" : ""; break; case Keys.NumPad6: String += nl ? "6" : ""; break; case Keys.NumPad7: String += nl ? "7" : ""; break; case Keys.NumPad8: String += nl ? "8" : ""; break; case Keys.NumPad9: String += nl ? "9" : ""; break; case Keys.Multiply: String += "*"; break; case Keys.Divide: String += "/"; break; case Keys.Add: String += "+"; break; case Keys.Subtract: String += "-"; break; case Keys.Decimal: String += "."; break; case Keys.Enter: String += Settings.ParseEnter ? "\n" : ""; break; case Keys.Tab: String += Settings.ParseTab ? "\t" : ""; break; default: break; } } // Limit Keyboard.String length if (String.Length > Settings.StoreLength) { String = String.Substring(String.Length - Settings.StoreLength); } } } }