Exemplo 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();
 }
Exemplo n.º 2
0
        // internal void AddKeyDown(AsciiKey key, Keys unmappedVirtualKey)
        // {
        //     KeysDownInternal.Add(key);
        //     //UnmappedVirtualKeysDown.Add(unmappedVirtualKey);
        // }

        internal void RemoveKeyDownAt(int i)
        {
            KeysDownInternal.RemoveAt(i);
            //UnmappedVirtualKeysDown.RemoveAt(i);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Returns true if the key is not in the <see cref="KeysDown"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key is not being pressed.</returns>
 public bool IsKeyUp(AsciiKey key)
 {
     return(!KeysDownInternal.Contains(key));
 }
Exemplo n.º 4
0
 /// <summary>
 /// Returns true if the key is not in the <see cref="KeysDown"/> collection regardless of shift state.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key is not being pressed.</returns>
 public bool IsKeyUp(Keys key)
 {
     return(!KeysDownInternal.Contains(AsciiKey.Get(key, _state)) && !KeysDownInternal.Contains(AsciiKey.Get(key, true, _state)));
 }
Exemplo n.º 5
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);
                }
            }
        }
Exemplo n.º 6
0
 // Always use the next routines to add or remove keys from KeysDownInternal or
 // UnmappedVirtualKeysDown.  This ensures that they stay parallel.
 private void AddKeyDown(AsciiKey key, Keys unmappedVirtualKey)
 {
     KeysDownInternal.Add(key);
     UnmappedVirtualKeysDown.Add(unmappedVirtualKey);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Returns true if the key is in the <see cref="KeysDown"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key is being pressed.</returns>
 public bool IsKeyDown(AsciiKey key) => KeysDownInternal.Contains(key);
Exemplo n.º 8
0
 /// <summary>
 /// Returns true if the key is in the <see cref="KeysDown"/> collection regardless of shift state.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key is being pressed.</returns>
 public bool IsKeyDown(Keys key) => KeysDownInternal.Contains(AsciiKey.Get(key, _state)) || KeysDownInternal.Contains(AsciiKey.Get(key, true, _state));
Exemplo n.º 9
0
 /// <summary>
 /// Returns true if the key is not in the <see cref="KeysDown"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key is not being pressed.</returns>
 public bool IsKeyUp(AsciiKey key) => !KeysDownInternal.Contains(key);
Exemplo n.º 10
0
 /// <summary>
 /// Returns true if the key is in the <see cref="KeysDown"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key is being pressed.</returns>
 public bool IsKeyDown(Keys key)
 {
     return(KeysDownInternal.Contains(AsciiKey.Get(key)));
 }