Represents the state of a single key.
Example #1
0
        /// <summary>
        /// Shortcut to get the <see cref="AsciiKey"/> for a specific MonoGame/XNA <see cref="Keys"/> type.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="shiftPressed">If shift should be considered pressed or not.</param>
        /// <returns>The <see cref="AsciiKey"/> of the <see cref="Keys"/>.</returns>
        public static AsciiKey Get(Keys key, bool shiftPressed)
        {
            AsciiKey asciiKey = new AsciiKey();

            asciiKey.Fill(key, shiftPressed);
            return(asciiKey);
        }
Example #2
0
        /// <summary>
        /// Shortcut to get the <see cref="AsciiKey"/> for a specific MonoGame/XNA <see cref="Keys"/> type. Shift is considered not pressed.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="state">Keyboar state to read from.</param>
        /// <returns>The <see cref="AsciiKey"/> of the <see cref="Keys"/>.</returns>
        public static AsciiKey Get(Keys key, IKeyboardState state)
        {
            AsciiKey asciiKey = new AsciiKey();

            asciiKey.Fill(key, false, state);
            return(asciiKey);
        }
Example #3
0
        /// <summary>
        /// Shortcut to get the <see cref="AsciiKey"/> for a specific MonoGame/XNA <see cref="Keys"/> type.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="shiftPressed">If shift should be considered pressed or not.</param>
        /// <param name="state">Keyboar state to read from.</param>
        /// <returns>The <see cref="AsciiKey"/> of the <see cref="Keys"/>.</returns>
        public static AsciiKey Get(Keys key, bool shiftPressed, IKeyboardState state)
        {
            AsciiKey asciiKey = new AsciiKey();

            asciiKey.Fill(key, shiftPressed, state);
            return(asciiKey);
        }
Example #4
0
        /// <summary>
        /// Shortcut to get the <see cref="AsciiKey"/> for a specific MonoGame/XNA <see cref="Keys"/> type. Shift is considered not pressed.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>The <see cref="AsciiKey"/> of the <see cref="Keys"/>.</returns>
        public static AsciiKey Get(Keys key)
        {
            AsciiKey asciiKey = new AsciiKey();

            asciiKey.Fill(key, false);
            return(asciiKey);
        }
Example #5
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)));
 }
Example #6
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);
                }
            }
        }
Example #7
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);
 }
Example #8
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));
 }
Example #9
0
 public static AsciiKey Get(Keys key, bool shiftPressed)
 {
     AsciiKey asciiKey = new AsciiKey();
     asciiKey.Fill(key, shiftPressed);
     return asciiKey;
 }
Example #10
0
 /// <summary>
 /// Returns true when the key is in the <see cref="KeysReleased"/> collection regardless of shift state.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key was released this update frame.</returns>
 public bool IsKeyReleased(Keys key) => KeysReleasedInternal.Contains(AsciiKey.Get(key, _state)) || KeysReleasedInternal.Contains(AsciiKey.Get(key, true, _state));
Example #11
0
 /// <summary>
 /// Returns true when the key is in the <see cref="KeysReleased"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key was released this update frame.</returns>
 public bool IsKeyReleased(AsciiKey key) => KeysReleasedInternal.Contains(key);
Example #12
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));
Example #13
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);
Example #14
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);
Example #15
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(Keys key)
 {
     return(!KeysDownInternal.Contains(AsciiKey.Get(key)));
 }
Example #16
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(Keys key)
 {
     return(KeysPressed.Contains(AsciiKey.Get(key)));
 }
Example #17
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));
 }
Example #18
0
        /// <summary>
        /// Reads the keyboard state using the <see cref="GameTime"/> from the update frame.
        /// </summary>
        /// <param name="gameTime"></param>
        public void ProcessKeys(GameTime gameTime)
        {
            this.KeysPressed.Clear();
            this.KeysReleased.Clear();

#if !SILVERLIGHT && !SHARPDX
            KeyboardState state        = Keyboard.GetState();
            bool          shiftPressed = state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift);
            var           keys         = state.GetPressedKeys();
#elif SHARPDX
            Keyboard      k     = new Keyboard(new DirectInput());
            KeyboardState state = k.GetCurrentState();
            k.Dispose();

            bool shiftPressed = state.IsPressed(Keys.LeftShift) || state.IsPressed(Keys.RightShift);
            var  keys         = state.PressedKeys;
#else
            KeyboardState state        = Keyboard.GetState();
            bool          shiftPressed = state.IsKeyDown(Keys.Shift);
            var           keys         = state.GetPressedKeys();
#endif



            // Cycle all the keys down known if any are up currently, remove
            for (int i = 0; i < this.KeysDown.Count;)
            {
#if SHARPDX
                if (!state.PressedKeys.Contains(this.KeysDown[i].XnaKey))
#else
                if (state.IsKeyUp(this.KeysDown[i].XnaKey))
#endif
                {
                    KeysReleased.Add(this.KeysDown[i]);
                    this.KeysDown.Remove(this.KeysDown[i]);
                }
                else
                {
                    i++;
                }
            }

            // For all new keys down, if we don't know them, add them to pressed, add them to down.
#if SHARPDX
            for (int i = 0; i < keys.Count; i++)
#else
            for (int i = 0; i < keys.Length; i++)
#endif
            {
                bool firstPressed = false;

                Input.AsciiKey key = new AsciiKey();
                Input.AsciiKey keyOppositeShift = new AsciiKey();
                Input.AsciiKey activeKey;

                key.Fill(keys[i], shiftPressed);
                keyOppositeShift.Fill(keys[i], !shiftPressed);

                if (this.KeysDown.Contains(key))
                {
                    activeKey           = this.KeysDown.First(k => k == key);
                    activeKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    this.KeysDown.Remove(key);
                }
                else if (this.KeysDown.Contains(keyOppositeShift))
                {
                    activeKey           = this.KeysDown.First(k => k == keyOppositeShift);
                    activeKey.Character = key.Character;
                    activeKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    this.KeysDown.Remove(keyOppositeShift);
                }
                else
                {
                    activeKey    = key;
                    firstPressed = true;
                }

                if (firstPressed)
                {
                    this.KeysPressed.Add(activeKey);
                }
                else if (activeKey.PreviouslyPressed == false && activeKey.TimeHeld >= InitialRepeatDelay)
                {
                    activeKey.PreviouslyPressed = true;
                    activeKey.TimeHeld          = 0f;
                    this.KeysPressed.Add(activeKey);
                }
                else if (activeKey.PreviouslyPressed == true && activeKey.TimeHeld >= RepeatDelay)
                {
                    activeKey.TimeHeld = 0f;
                    this.KeysPressed.Add(activeKey);
                }

                this.KeysDown.Add(activeKey);
            }
        }
Example #19
0
        /// <summary>
        /// Reads the keyboard state using the <see cref="GameTime"/> from the update frame.
        /// </summary>
        /// <param name="gameTime"></param>
        public void ProcessKeys(GameTime gameTime)
        {
            this.KeysPressed.Clear();
            this.KeysReleased.Clear();

            // Cycle all the keys down known if any are up currently, remove
            KeyboardState state = Keyboard.GetState();
            bool shiftPressed = state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift);
            var keys = state.GetPressedKeys();
            for (int i = 0; i < this.KeysDown.Count;)
            {
                if (state.IsKeyUp(this.KeysDown[i].XnaKey))
                {
                    KeysReleased.Add(this.KeysDown[i]);
                    this.KeysDown.Remove(this.KeysDown[i]);
                }
                else
                    i++;
            }

            // For all new keys down, if we don't know them, add them to pressed, add them to down.
            for (int i = 0; i < keys.Length; i++)
            {
                bool firstPressed = false;

                AsciiKey key = new AsciiKey();
                AsciiKey keyOppositeShift = new AsciiKey();
                AsciiKey activeKey;

                key.Fill(keys[i], shiftPressed);
                keyOppositeShift.Fill(keys[i], !shiftPressed);

                if (this.KeysDown.Contains(key))
                {
                    activeKey = this.KeysDown.First(k => k == key);
                    activeKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    this.KeysDown.Remove(key);
                }
                else if (this.KeysDown.Contains(keyOppositeShift))
                {
                    activeKey = this.KeysDown.First(k => k == keyOppositeShift);
                    activeKey.Character = key.Character;
                    activeKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    this.KeysDown.Remove(keyOppositeShift);
                }
                else
                {
                    activeKey = key;
                    firstPressed = true;
                }

                if (firstPressed)
                {
                    this.KeysPressed.Add(activeKey);
                }
                else if (activeKey.PreviouslyPressed == false && activeKey.TimeHeld >= InitialRepeatDelay)
                {
                    activeKey.PreviouslyPressed = true;
                    activeKey.TimeHeld = 0f;
                    this.KeysPressed.Add(activeKey);
                }
                else if (activeKey.PreviouslyPressed == true && activeKey.TimeHeld >= RepeatDelay)
                {
                    activeKey.TimeHeld = 0f;
                    this.KeysPressed.Add(activeKey);
                }

                this.KeysDown.Add(activeKey);
            }
        }
Example #20
0
 /// <summary>
 /// Returns true when they is in the <see cref="KeysReleased"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key was released this update frame.</returns>
 public bool IsKeyReleased(Keys key)
 {
     return(KeysReleased.Contains(AsciiKey.Get(key)));
 }
Example #21
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);
Example #22
0
 public static AsciiKey Get(Keys key)
 {
     AsciiKey asciiKey = new AsciiKey();
     asciiKey.Fill(key, false);
     return asciiKey;
 }
Example #23
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(KeysDown.Contains(AsciiKey.Get(key)));
 }
Example #24
0
 /// <summary>
 /// Returns true when the key is in the <see cref="KeysReleased"/> collection.
 /// </summary>
 /// <param name="key">The key to check.</param>
 /// <returns>True when the key was released this update frame.</returns>
 public bool IsKeyReleased(AsciiKey key)
 {
     return(KeysReleasedInternal.Contains(key));
 }
Example #25
0
        /// <summary>
        /// Reads the keyboard state using the <see cref="GameTime"/> from the update frame.
        /// </summary>
        /// <param name="gameTime"></param>
        public void ProcessKeys(GameTime gameTime)
        {
            this.KeysPressed.Clear();
            this.KeysReleased.Clear();

            // Cycle all the keys down known if any are up currently, remove

#if SFML
            bool shiftPressed = SFML.Window.Keyboard.IsKeyPressed(Keys.LShift) || SFML.Window.Keyboard.IsKeyPressed(Keys.RShift);
            for (int i = 0; i < this.KeysDown.Count;)
            {
                if (!SFML.Window.Keyboard.IsKeyPressed(this.KeysDown[i].XnaKey))
                {
                    tempKeysDown.Remove(this.KeysDown[i].XnaKey);
                    KeysReleased.Add(this.KeysDown[i]);
                    this.KeysDown.Remove(this.KeysDown[i]);
                }
                else
                {
                    i++;
                }
            }
            var keys = tempKeysDown.ToArray();
#elif MONOGAME
            KeyboardState state        = Keyboard.GetState();
            bool          shiftPressed = state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift);
            var           keys         = state.GetPressedKeys();
            for (int i = 0; i < this.KeysDown.Count;)
            {
                if (state.IsKeyUp(this.KeysDown[i].XnaKey))
                {
                    KeysReleased.Add(this.KeysDown[i]);
                    this.KeysDown.Remove(this.KeysDown[i]);
                }
                else
                {
                    i++;
                }
            }
#endif

            // For all new keys down, if we don't know them, add them to pressed, add them to down.
            for (int i = 0; i < keys.Length; i++)
            {
                bool firstPressed = false;

                AsciiKey key = new AsciiKey();
                AsciiKey keyOppositeShift = new AsciiKey();
                AsciiKey activeKey;

                key.Fill(keys[i], shiftPressed);
                keyOppositeShift.Fill(keys[i], !shiftPressed);

                if (this.KeysDown.Contains(key))
                {
                    activeKey           = this.KeysDown.First(k => k == key);
                    activeKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    this.KeysDown.Remove(key);
                }
                else if (this.KeysDown.Contains(keyOppositeShift))
                {
                    activeKey           = this.KeysDown.First(k => k == keyOppositeShift);
                    activeKey.Character = key.Character;
                    activeKey.TimeHeld += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    this.KeysDown.Remove(keyOppositeShift);
                }
                else
                {
                    activeKey    = key;
                    firstPressed = true;
                }

                if (firstPressed)
                {
                    this.KeysPressed.Add(activeKey);
                }
                else if (activeKey.PreviouslyPressed == false && activeKey.TimeHeld >= InitialRepeatDelay)
                {
                    activeKey.PreviouslyPressed = true;
                    activeKey.TimeHeld          = 0f;
                    this.KeysPressed.Add(activeKey);
                }
                else if (activeKey.PreviouslyPressed == true && activeKey.TimeHeld >= RepeatDelay)
                {
                    activeKey.TimeHeld = 0f;
                    this.KeysPressed.Add(activeKey);
                }

                this.KeysDown.Add(activeKey);
            }
        }
Example #26
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)));
 }