Exemplo n.º 1
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            List <Keys> keyboardState = Keyboard.GetState().GetPressedKeys().ToList <Keys>();

            foreach (Keys key in keyboardState)
            {
                //We can assume the key is pressed while looping through this list.

                KeyTimer keyTimer = pressedKeys.Find(x => x.Key == key);
                if (keyTimer != null)
                {
                    //Key is still pressed, update its timer.
                    keyTimer.Update(gameTime.ElapsedGameTime);
                }
                else
                {
                    //Key is newly pressed, so add it to the list.
                    pressedKeys.Add(new KeyTimer(key));
                }
            }

            //Remove all keys that are no longer pressed.
            pressedKeys.RemoveAll(a => !keyboardState.Exists(x => x == a.Key));

            base.Update(gameTime);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if the key was just pressed, or has been pressed in a previous cycle.
        /// </summary>
        /// <param name="key">The key that needs to be checked.</param>
        /// <returns></returns>
        public bool IsNewKeyPress(Keys key, bool isRepeatAble)
        {
            KeyTimer pressedKey = pressedKeys.Find(x => x.Key == key);

            if (pressedKey == null)
            {
                //Key is not pressed at all
                return(false);
            }
            else
            {
                //Key is pressed, check if it should register as a new press.
                if ((!isRepeatAble && pressedKey.TimePressed == TimeSpan.Zero)
                    ||
                    (isRepeatAble && pressedKey.TimePressed >= Settings.RepeatRate || pressedKey.TimePressed == TimeSpan.Zero))
                {
                    //A new press has been detected, reset the timer for the key.
                    pressedKey.ResetTimer();
                    return(true);
                }
                else
                {
                    //The key was pressed, but it does not register as a new press.
                    return(false);
                }
            }
        }