/// <summary> /// Creates a new KeyboardEventArgs, given a time for the event, the key that was pressed, and /// the modifiers that were applied at the time of the press, as well as the keyboard state at /// the time the event occurred. /// </summary> public KeyboardEventArgs(TimeSpan time, Keys key, Modifiers modifiers, KeyboardState state) : base(time) { Character = KeyboardUtil.ToChar(key, modifiers); State = state; Modifiers = modifiers; Key = key; }
private void OnCharacterPressed(object sender, KeyboardKeyEventArgs args) { if (CharacterTyped == null) { return; } var character = KeyboardUtil.ToChar(args.Key, args.Modifiers); if (character.HasValue) { CharacterTyped(this, new KeyboardCharacterEventArgs(character.Value)); } }
/// <summary> /// Updates the component, turning XNA's polling model into an event-based model, raising /// events as they happen. /// </summary> /// <param name="gameTime"></param> public void Update(GameTime gameTime) { var current = Keyboard.GetState(); // Build the modifiers that currently apply to the current situation. var modifiers = Modifiers.None; if (current.IsKeyDown(Keys.LeftControl) || current.IsKeyDown(Keys.RightControl)) { modifiers |= Modifiers.Control; } if (current.IsKeyDown(Keys.LeftShift) || current.IsKeyDown(Keys.RightShift)) { modifiers |= Modifiers.Shift; } if (current.IsKeyDown(Keys.LeftAlt) || current.IsKeyDown(Keys.RightAlt)) { modifiers |= Modifiers.Alt; } // Key pressed and initial key typed events for all keys. foreach (Keys key in Enum.GetValues(typeof(Keys))) { if (current.IsKeyDown(key) && _previous.IsKeyUp(key)) { OnKeyPressed(this, new KeyEventArgs(gameTime.TotalGameTime, key, modifiers, current)); var ch = KeyboardUtil.ToChar(key, modifiers); if (ch.HasValue) { OnKeyTyped(this, new CharacterEventArgs(gameTime.TotalGameTime, ch.Value, modifiers, current)); } // Maintain the state of last key pressed. _lastKey = key; _lastPress = gameTime.TotalGameTime; _isInitial = true; } } // Key released events for all keys. foreach (Keys key in Enum.GetValues(typeof(Keys))) { if (current.IsKeyUp(key) && _previous.IsKeyDown(key)) { OnKeyReleased(this, new KeyEventArgs(gameTime.TotalGameTime, key, modifiers, current)); } } // Handle keys being held down and getting multiple KeyTyped events in sequence. var elapsedTime = (gameTime.TotalGameTime - _lastPress).TotalMilliseconds; if (current.IsKeyDown(_lastKey) && ((_isInitial && elapsedTime > InitialDelay) || (!_isInitial && elapsedTime > RepeatDelay))) { var ch = KeyboardUtil.ToChar(_lastKey, modifiers); if (ch.HasValue) { OnKeyTyped(this, new CharacterEventArgs(gameTime.TotalGameTime, ch.Value, modifiers, current)); _lastPress = gameTime.TotalGameTime; _isInitial = false; } } _previous = current; }
private static void UpdateKeyboardEvents(GameTime gameTime) { KeyboardState current = Keyboard.GetState(); // Build the modifiers that currently apply to the current situation. var modifiers = Modifiers.None; if (current.IsKeyDown(Keys.LeftControl) || current.IsKeyDown(Keys.RightControl)) { modifiers |= Modifiers.Control; } if (current.IsKeyDown(Keys.LeftShift) || current.IsKeyDown(Keys.RightShift)) { modifiers |= Modifiers.Shift; } if (current.IsKeyDown(Keys.LeftAlt) || current.IsKeyDown(Keys.RightAlt)) { modifiers |= Modifiers.Alt; } // Key pressed and initial key typed events for all keys. foreach (Keys key in Enum.GetValues(typeof(Keys))) { if (current.IsKeyDown(key) && _previousKeyboardState.IsKeyUp(key)) { RaiseKeyDownEvent(key, modifiers); char?keyChar = KeyboardUtil.ToChar(key, modifiers); if (keyChar.HasValue) { RaiseCharacterEvent(keyChar.Value); } // Maintain the state of last key pressed. _lastKey = key; _lastPress = gameTime.TotalGameTime; _isInitial = true; } if (current.IsKeyUp(key) && _previousKeyboardState.IsKeyDown(key)) { RaiseKeyUpEvent(key, modifiers); } } // Handle keys being held down and getting multiple KeyTyped events in sequence. var elapsedTime = (gameTime.TotalGameTime - _lastPress).TotalMilliseconds; if (current.IsKeyDown(_lastKey) && ((_isInitial && elapsedTime > _initialDelay) || (!_isInitial && elapsedTime > _repeatDelay))) { RaiseKeyDownEvent(_lastKey, modifiers); char?lastKeyChar = KeyboardUtil.ToChar(_lastKey, modifiers); if (lastKeyChar.HasValue) { RaiseCharacterEvent(lastKeyChar.Value); _lastPress = gameTime.TotalGameTime; _isInitial = false; } } _previousKeyboardState = current; }