Fill() public method

Fills out the fields based on the MonoGame/XNA key.
public Fill ( Keys key, bool shiftPressed ) : void
key Keys The key.
shiftPressed bool Helps identify which to use while the key is pressed. For example, if is used the field will be either 'A' if is true or 'a' if false.
return void
Esempio n. 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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 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>
        /// <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);
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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);
            }
        }
Esempio n. 7
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);
            }
        }
Esempio n. 8
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. 9
0
 public static AsciiKey Get(Keys key, bool shiftPressed)
 {
     AsciiKey asciiKey = new AsciiKey();
     asciiKey.Fill(key, shiftPressed);
     return asciiKey;
 }
Esempio n. 10
0
 public static AsciiKey Get(Keys key)
 {
     AsciiKey asciiKey = new AsciiKey();
     asciiKey.Fill(key, false);
     return asciiKey;
 }