Esempio n. 1
0
 /// <summary>
 /// Clears the <see cref="KeysPressed"/>, <see cref="KeysDown"/>, <see cref="KeysReleased"/> collections.
 /// </summary>
 public void Clear()
 {
     KeysPressedInternal.Clear();
     KeysDownInternal.Clear();
     UnmappedVirtualKeysDown.Clear();
     KeysReleasedInternal.Clear();
 }
Esempio n. 2
0
 internal void RemoveKeyPressed(int i)
 {
     KeysPressedInternal.RemoveAt(i);
 }
Esempio n. 3
0
 internal void AddKeyPressed(AsciiKey key, Keys unmappedVirtualKey)
 {
     KeysPressedInternal.Add(key);
 }
Esempio n. 4
0
        /// <summary>
        /// Reads the keyboard state using the <see cref="GameTime"/> from the update frame.
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            KeysPressedInternal.Clear();
            KeysReleasedInternal.Clear();

            // Cycle all the keys down known if any are up currently, remove them from KeysDownInternal
            _state = Microsoft.Xna.Framework.Input.Keyboard.GetState();
            bool shiftPressed        = _state.IsKeyDown(Keys.LeftShift) || _state.IsKeyDown(Keys.RightShift);
            var  unmappedVirtualKeys = _state.GetPressedKeys();

            for (int i = 0; i < KeysDownInternal.Count;)
            {
                if (_state.IsKeyUp(UnmappedVirtualKeysDown[i]))
                {
                    KeysReleasedInternal.Add(KeysDownInternal[i]);
                    RemoveKeyDownAt(i);
                }
                else
                {
                    i++;
                }
            }

            // KeysDownInternal now contains all the keys that are currently down except potentially newly pressed keys
            // however the shift state may have changed so if there was an 'a' previously and the shift key was
            // just pressed, then the 'a' needs to get replaced with 'A'.

            // For all new keys down, if we don't know them, add them to pressed, add them to down.
            foreach (var unmappedVirtualKey in unmappedVirtualKeys)
            {
                bool firstPressed = false;

                AsciiKey key = new AsciiKey();
                AsciiKey keyOppositeShift = new AsciiKey();
                int      activeKeyIndex   = -1;

                // These keys will be mapped since Fill does the mapping automatically
                key.Fill(unmappedVirtualKey, shiftPressed, _state);
                keyOppositeShift.Fill(unmappedVirtualKey, !shiftPressed, _state);

                if (KeysDownInternal.Contains(key))
                {
                    activeKeyIndex = KeysDownInternal.FindIndex(k => k == key);
                    var thisKey = KeysDownInternal[activeKeyIndex];
                    thisKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    KeysDownInternal[activeKeyIndex] = thisKey;
                }
                else if (KeysDownInternal.Contains(keyOppositeShift))
                {
                    activeKeyIndex = KeysDownInternal.FindIndex(k => k == keyOppositeShift);
                    var thisKey = KeysDownInternal[activeKeyIndex];
                    thisKey.Character = key.Character;
                    thisKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    KeysDownInternal[activeKeyIndex] = thisKey;
                }
                else
                {
                    // The physical key (independent of shift) for this character was not being pressed before
                    // so add it in.
                    firstPressed   = true;
                    activeKeyIndex = KeysDownInternal.Count;
                    AddKeyDown(key, unmappedVirtualKey);
                }

                var activeKey = KeysDownInternal[activeKeyIndex];

                if (firstPressed)
                {
                    KeysPressedInternal.Add(activeKey);
                }
                else if (activeKey.PostInitialDelay == false && activeKey.TimeHeld >= InitialRepeatDelay)
                {
                    activeKey.PostInitialDelay = true;
                    activeKey.TimeHeld         = 0f;
                    KeysPressedInternal.Add(activeKey);
                }
                else if (activeKey.PostInitialDelay && activeKey.TimeHeld >= RepeatDelay)
                {
                    activeKey.TimeHeld = 0f;
                    KeysPressedInternal.Add(activeKey);
                }
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Returns true when the key is in the <see cref="KeysPressed"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key was considered first pressed.</returns>
 public bool IsKeyPressed(AsciiKey key)
 {
     return(KeysPressedInternal.Contains(key));
 }
Esempio n. 6
0
 /// <summary>
 /// Returns true when the key is in the <see cref="KeysPressed"/> collection regardless of shift state.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key was considered first pressed.</returns>
 public bool IsKeyPressed(Keys key)
 {
     return(KeysPressedInternal.Contains(AsciiKey.Get(key, _state)) || KeysPressedInternal.Contains(AsciiKey.Get(key, true, _state)));
 }
Esempio n. 7
0
 /// <summary>
 /// Returns true when the key is in the <see cref="KeysPressed"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key was considered first pressed.</returns>
 public bool IsKeyPressed(AsciiKey key) => KeysPressedInternal.Contains(key);