Exemplo n.º 1
0
        private void FireKeyboardEvents()
        {
            // Check through each key in the key list
            foreach (Keys key in KeyList)
            {
                // Is the key currently down?
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    // Fire the OnKeyDown event
                    OnKeyDown?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }

                // Has the key been released? (Was down and is now up)
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    OnKeyUp?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }

                // Has the key been pressed? (Was up and is now down)
                if (PrevKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    OnKeyPressed?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }
            }
        }
        private void FireKeyboardEvents()
        {
            foreach (Keys key in KeyList)
            {
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    if (OnKeyDown != null)
                    {
                        OnKeyDown(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }

                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    if (OnKeyUp != null)
                    {
                        OnKeyUp(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }

                if (PrevKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    if (OnKeyPressed != null)
                    {
                        OnKeyPressed(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }
            }
        }
Exemplo n.º 3
0
        private void FireKeyboardEvents()
        {
            foreach (var key in KeyList)
            {
                // Is the key currently down?
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    // Fire the OnKeyDown event
                    if (OnKeyDown != null)
                    {
                        OnKeyDown(this, new KeyboardEventArgs(key, CurrentKeyboardState,
                                                              PrevKeyboardState));
                    }
                }

                // Has the key been released? (Was down and is now up)
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    // Fire the OnKeyUp event
                    if (OnKeyUp != null)
                    {
                        OnKeyUp(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }

                // Has the key been pressed? (Was up and is now down)
                if (PrevKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    if (OnKeyPressed != null)
                    {
                        OnKeyPressed(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }
            }
        }
Exemplo n.º 4
0
        public Keys[] GetKeysPressed()
        {
            var         keys        = CurrentKeyboardState.GetPressedKeys();
            var         pkeys       = PreviousKeyboardState.GetPressedKeys();
            List <Keys> pressedKeys = new List <Keys>();

            if (active)
            {
                foreach (var key in keys)
                {
                    bool pressed = true;
                    foreach (var pkey in pkeys)
                    {
                        if (pkey == key)
                        {
                            pressed = false;
                        }
                    }
                    if (pressed)
                    {
                        pressedKeys.Add(key);
                    }
                }
            }

            return(pressedKeys.ToArray());
        }
Exemplo n.º 5
0
 public bool KeyDown(Keys key)
 {
     if (active)
     {
         return(CurrentKeyboardState.IsKeyDown(key));
     }
     return(false);
 }
Exemplo n.º 6
0
        /// <summary>
        /// 是否按了某个键
        /// </summary>
        /// <param name="key"></param>

        public static bool IsKeyPressed(Keys key)
        {
            if (previousKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 7
0
        /// <summary>
        /// returns true when a key was pressed once
        /// </summary>
        /// <returns></returns>
        public bool KeyPressed(Keys key)
        {
            if (CurrentKeyboardState.IsKeyDown(key) && PrevioKeyboardState.IsKeyUp(key))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Used for determining if a keyType is being pressed.
        /// </summary>
        /// <param name="key">Key to check if pressed.</param>
        /// <returns>Returns true if keyType is down.</returns>
        public static bool KeyPressed(Keys key)
        {
            if (_active && CurrentKeyboardState.IsKeyDown(key))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Used for determining if a keyType was just released.
        /// </summary>
        /// <param name="key">Key to check if released.</param>
        /// <returns>Returns true if keyType is up and last frame is down.</returns>
        public static bool KeyJustReleased(Keys key)
        {
            if (_active && OldKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
            {
                return(true);
            }

            return(false);
        }
Exemplo n.º 10
0
        //überprüft tastaturinput und schreib sie in den string, gibt über Flags zurück welche Tasten gedrückt wurden (im moment nur Enter)
        static public byte WriteInput(ref string changingText)
        {
            Keys[] lk           = LastKeyboardState.GetPressedKeys();
            Keys[] ck           = CurrentKeyboardState.GetPressedKeys();
            Keys[] k            = ck.Except(lk).ToArray();
            byte   code         = 0;
            string tmpstring    = "";
            bool   shiftpressed = CurrentKeyboardState.IsKeyDown(Keys.RightShift) || CurrentKeyboardState.IsKeyDown(Keys.LeftShift);
            bool   backpressed  = false;

            for (int i = 0; i < k.Length; i++)
            {
                switch (k[i])
                {
                case Keys.Back: backpressed = true;
                    break;

                case Keys.OemPeriod: tmpstring += ".";
                    break;

                case Keys.Space: tmpstring += " ";
                    break;

                //Enter Flag Setzen
                case Keys.Enter: code |= 0x01;
                    break;

                case Keys.LeftShift:
                case Keys.RightShift: shiftpressed = true;
                    break;

                default:
                    //Buchstabe zum string hinzufügen fall er sich im erlaubten Ascii bereich befindet
                    if ((int)k[i] > 32 && (int)k[i] < 128)
                    {
                        tmpstring += (char)k[i];
                    }
                    break;
                }
            }
            if (!shiftpressed)
            {
                changingText += tmpstring.ToLower();
            }
            else
            {
                changingText += tmpstring.ToUpper();
            }

            if (backpressed && changingText != "")
            {
                //Lösche Letztes Zeichen
                changingText = changingText.Substring(0, changingText.Length - 1);
            }
            return(code);
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Handles input for quitting the game.
        /// </summary>
        private void HandleInput()
        {
            CurrentKeyboardState = Keyboard.GetState();

            // Check for exit.
            if (CurrentKeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Returns all the keys which were not down last frame, but are up now
        /// </summary>
        /// <returns></returns>
        public Keys[] GetPressedKeys()
        {
            if (!calculatedPressedKeys)
            {
                // Get all the keys in thisFrame that weren't in last frame
                PressedKeys           = Array.FindAll(CurrentKeyboardState.GetPressedKeys(), x => Array.IndexOf(PreviousKeyboardState.GetPressedKeys(), x) == -1);
                calculatedPressedKeys = true;
            }

            return(PressedKeys);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Wurde die aktuelle Taste losgelassen und war sie im letzten Frame noch gedrückt?
 /// </summary>
 public bool KeyReleased(Keys key)
 {
     // Is the key up?
     if (!CurrentKeyboardState.IsKeyDown(key))
     {
         // If down last update, key has just been released.
         if (PreviousKeyboardState.IsKeyDown(key))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Wurde die aktuelle Taste gedrückt und war sie im letzten Frame nicht gedrückt?
 /// </summary>
 public bool KeyPressed(Keys key)
 {
     // Is the key down?
     if (CurrentKeyboardState.IsKeyDown(key))
     {
         // If not down last update, key has just been pressed.
         if (!PreviousKeyboardState.IsKeyDown(key))
         {
             return(true);
         }
     }
     return(false);
 }
Exemplo n.º 15
0
        internal bool IsKeyReleased(Keys key)
        {
            if (key == Keys.None)
            {
                return(true);
            }

            if (CurrentKeyboardState == null || PreviousKeyboardState == null)
            {
                return(false);
            }

            return((!CurrentKeyboardState.IsKeyDown(key)) && (PreviousKeyboardState.IsKeyDown(key)));
        }
Exemplo n.º 16
0
        public Keys GetDownedKey(List <Keys> acceptOnlyTheseKeys) // returns first key found. only applies if the key is still down in this frame/cycle
        {
            List <Keys> keysdown = new List <Keys>(CurrentKeyboardState.GetPressedKeys());

            keysdown.Remove(Keys.None);

            foreach (Keys key in acceptOnlyTheseKeys)
            {
                if (keysdown.Contains(key))
                {
                    return(key);
                }
            }

            return(Keys.None);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Gets the current number key pressed, returns -1 if none
        /// </summary>
        public int GetDigitPressed()
        {
            var pressedDigitKeys = CurrentKeyboardState.GetPressedKeys()
                                   // Select those that are within D0 and D9
                                   .Where(x => x >= Keys.D0 && x <= Keys.D9)
                                   // Select those that weren't pressed last time
                                   .Where(x => !PreviousKeyboardState.GetPressedKeys().Contains(x)).ToArray();

            if (pressedDigitKeys.Length == 0)
            {
                return(-1);
            }

            // D0 is 9, D1 is 0, D2 is 1, and so on...
            return(pressedDigitKeys[0] == Keys.D0 ? 0 : ((int)pressedDigitKeys[0]) - 48);
        }
Exemplo n.º 18
0
 //Gibt die Zahl der Taste zurück die gerade losgelassen wurde, bei mehreren wird die niedrigere Zurückgegeben
 //0 Entspricht 10!
 //Falls keine Zahlentaste (!)  gedrückt wurde wird -1 zurückgeben
 //Zahlentasten des NumPads werden nicht berücksichtigt
 static public int GetNumberPressed()
 {
     if (!LastKeyboardState.IsKeyDown(Keys.D1) && CurrentKeyboardState.IsKeyDown(Keys.D1))
     {
         return(1);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D2) && CurrentKeyboardState.IsKeyDown(Keys.D2))
     {
         return(2);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D3) && CurrentKeyboardState.IsKeyDown(Keys.D3))
     {
         return(3);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D4) && CurrentKeyboardState.IsKeyDown(Keys.D4))
     {
         return(4);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D5) && CurrentKeyboardState.IsKeyDown(Keys.D5))
     {
         return(5);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D6) && CurrentKeyboardState.IsKeyDown(Keys.D6))
     {
         return(6);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D7) && CurrentKeyboardState.IsKeyDown(Keys.D7))
     {
         return(7);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D8) && CurrentKeyboardState.IsKeyDown(Keys.D8))
     {
         return(8);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D9) && CurrentKeyboardState.IsKeyDown(Keys.D9))
     {
         return(9);
     }
     if (!LastKeyboardState.IsKeyDown(Keys.D0) && CurrentKeyboardState.IsKeyDown(Keys.D0))
     {
         return(10);
     }
     return(-1);
 }
Exemplo n.º 19
0
        public List <Keys> GetAllReleasedKeys() // downed in prev state and now released
        {
            List <Keys> prevKeysPressed    = new List <Keys>(PrevKeyboardState.GetPressedKeys());
            List <Keys> currentKeysPressed = new List <Keys>(CurrentKeyboardState.GetPressedKeys());

            prevKeysPressed.Remove(Keys.None);
            currentKeysPressed.Remove(Keys.None);

            List <Keys> result = new List <Keys>();

            foreach (Keys key in prevKeysPressed)
            {
                if (!currentKeysPressed.Contains(key)) // dus als de oude list een key heeft die de nieuwe niet heeft dan is die dus released
                {
                    result.Add(key);
                }
            }
            return(result);
        }
Exemplo n.º 20
0
        public void Update()
        {
            PreviousKeyboardState = CurrentKeyboardState;
            CurrentKeyboardState  = Keyboard.GetState();

            DownKeys.Clear();
            PressedKeys.Clear();
            Keys[] keys = CurrentKeyboardState.GetPressedKeys(); // TODO: per frame heap allocs
            foreach (Keys key in keys)
            {
                if (PreviousKeyboardState.IsKeyUp(key) && IsKeyDown(key))
                {
                    PressedKeys.Add(key);
                }
                //else //if (_previousKeyState.IsKeyDown(key) && _currentKeyState.IsKeyDown(key))
                //    DownKeys.Add(key);
                if (IsKeyDown(key))
                {
                    DownKeys.Add(key);
                }
            }
        }
Exemplo n.º 21
0
        //
        // Events that should be fired based on the interaction/input received

        private void FireKeyboardEvents()
        {
            foreach (Keys key in KeyList)
            {
                // Is key currently down?
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    OnKeyDown?.Invoke(key);
                }

                // Has the key been released?
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    OnKeyUp?.Invoke(key);
                }

                // Key has been held
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    OnKeyPressed?.Invoke(key);
                }
            }
        }
Exemplo n.º 22
0
 /// <summary>
 /// returns if the current key is down
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public bool KeyDown(Keys key)
 {
     return(CurrentKeyboardState.IsKeyDown(key));
 }
Exemplo n.º 23
0
 public static bool KeyReleased(Keys key)
 {
     return(CurrentKeyboardState.IsKeyUp(key) && OldKeyboardState.IsKeyDown(key));
 }
Exemplo n.º 24
0
 /// <summary>
 /// Queries the current and previous keyboard states to work out whether a key has been pressed
 /// </summary>
 /// <param name="key">The key we wish to query</param>
 /// <returns>Returns true if the key is down this frame and up the previous frame</returns>
 public bool IsKeyPressed(Keys key)
 {
     return(CurrentKeyboardState.IsKeyDown(key) && PreviousKeyboardState.IsKeyUp(key));
 }
Exemplo n.º 25
0
 /// <summary>
 /// Returns all the keys which are currently down
 /// </summary>
 /// <returns></returns>
 public Keys[] GetKeysDown()
 {
     return(CurrentKeyboardState.GetPressedKeys());
 }
Exemplo n.º 26
0
 public static bool IsKeyPressed(Keys key)
 {
     return(CurrentKeyboardState.IsKeyDown(key));
 }
Exemplo n.º 27
0
 public static bool WasJustPressed(Keys key)
 {
     return(OldKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key));
 }
Exemplo n.º 28
0
 public static bool WasKeyJustReleased(Keys key)
 {
     return(PreviousKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key));
 }
Exemplo n.º 29
0
 public static bool IsKeyPressed(Keys k) => CurrentKeyboardState.IsKeyDown(k) && !OldKeyboardState.IsKeyDown(k);
Exemplo n.º 30
0
        /// <summary>
        /// Update method
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Update(GameTime gameTime)
        {
            if (this.Name == "OptionsSelectionFrame")
            {
                string currentPosition = null;

                foreach (KeyValuePair <string, Vector2> keyValuePair in optionMenuDictionary)
                {
                    if ((
                            (keyValuePair.Value.X - 20 == this.DrawLocation.X)
                            ) && (
                            (keyValuePair.Value.Y - 15 == this.DrawLocation.Y)
                            ) && (
                            (keyValuePair.Key != "OptionSelectionFrame")
                            ))
                    {
                        currentPosition = keyValuePair.Key;                                 // Find the current index of the selection bar
                    }
                }
                // Move the selection bar based on key pressed
                if (CurrentKeyboardState.IsKeyDown(Keys.Down) && PreviousKeyboardState.IsKeyUp(Keys.Down))
                {
                    SoundEffectInstances["MenuMove"].Play();

                    switch (currentPosition)
                    {
                    case "Music":
                        base.CreateRectangle(
                            (int)(optionMenuDictionary["SFX"].X - 20),
                            (int)(optionMenuDictionary["SFX"].Y - 15)
                            );

                        break;
                    }
                }
                else if (CurrentKeyboardState.IsKeyDown(Keys.Up) && PreviousKeyboardState.IsKeyUp(Keys.Up))
                {
                    SoundEffectInstances["MenuMove"].Play();

                    switch (currentPosition)
                    {
                    case "SFX":
                        base.CreateRectangle(
                            (int)(optionMenuDictionary["Music"].X - 20),
                            (int)(optionMenuDictionary["Music"].Y - 15)
                            );

                        break;
                    }
                }
                else if (CurrentKeyboardState.IsKeyDown(Keys.Right) && PreviousKeyboardState.IsKeyUp(Keys.Right))
                {
                    switch (currentPosition)
                    {
                    case "Music":
                        if (SoundEffectInstances["BackgroundMusic"].Volume < 0.9f)          // Move the side scrolling volume controls

                        {
                            SoundEffectInstances["BackgroundMusic"].Volume += 0.1f;

                            foreach (Option menuOption in OptionElements)
                            {
                                if (menuOption.Name == "MusicSlider")
                                {
                                    menuOption.CreateRectangle(                             // Adjust the volume
                                        (int)(optionMenuDictionary["MusicSlider"].X + 10),
                                        (int)(optionMenuDictionary["MusicSlider"].Y)
                                        );

                                    optionMenuDictionary["MusicSlider"] = new Vector2(
                                        (int)(optionMenuDictionary["MusicSlider"].X + 10),
                                        (int)(optionMenuDictionary["MusicSlider"].Y)
                                        );

                                    SoundEffectInstances["MenuMove"].Play();
                                }
                            }
                        }
                        else
                        {
                            SoundEffectInstances["Error"].Play();
                        }

                        break;

                    case "SFX":
                        bool movedSlider = false;

                        foreach (KeyValuePair <string, SoundEffectInstance> keyValuePair in SoundEffectInstances)
                        {
                            if (keyValuePair.Key != "BackgroundMusic")
                            {
                                if (keyValuePair.Value.Volume < 0.9f)                       // Move the side scrolling volume controls
                                {
                                    keyValuePair.Value.Volume += 0.1f;

                                    if (!movedSlider)
                                    {
                                        foreach (Option menuOption in OptionElements)
                                        {
                                            if (menuOption.Name == "SFXSlider")
                                            {
                                                menuOption.CreateRectangle(                 // Adjust the volume
                                                    (int)(optionMenuDictionary["SFXSlider"].X + 10),
                                                    (int)(optionMenuDictionary["SFXSlider"].Y)
                                                    );

                                                optionMenuDictionary["SFXSlider"] = new Vector2(
                                                    (int)(optionMenuDictionary["SFXSlider"].X + 10),
                                                    (int)(optionMenuDictionary["SFXSlider"].Y)
                                                    );

                                                SoundEffectInstances["MenuMove"].Play();
                                            }
                                        }

                                        movedSlider = true;
                                    }
                                }
                                else
                                {
                                    SoundEffectInstances["Error"].Play();
                                }
                            }
                        }

                        break;

                    case "Difficulty":
                        break;
                    }
                }
                else if (CurrentKeyboardState.IsKeyDown(Keys.Left) && PreviousKeyboardState.IsKeyUp(Keys.Left))
                {
                    switch (currentPosition)
                    {
                    case "Music":
                        if (SoundEffectInstances["BackgroundMusic"].Volume > 0.1f)          // Move the side scrolling volume controls
                        {
                            SoundEffectInstances["BackgroundMusic"].Volume -= 0.1f;

                            foreach (Option menuOption in OptionElements)
                            {
                                if (menuOption.Name == "MusicSlider")
                                {
                                    menuOption.CreateRectangle(                             // Adjust the volume
                                        (int)(optionMenuDictionary["MusicSlider"].X - 10),
                                        (int)(optionMenuDictionary["MusicSlider"].Y)
                                        );

                                    optionMenuDictionary["MusicSlider"] = new Vector2(
                                        (int)(optionMenuDictionary["MusicSlider"].X - 10),
                                        (int)(optionMenuDictionary["MusicSlider"].Y)
                                        );

                                    SoundEffectInstances["MenuMove"].Play();
                                }
                            }
                        }
                        else
                        {
                            SoundEffectInstances["Error"].Play();
                        }

                        break;

                    case "SFX":
                        bool movedSlider = false;

                        foreach (KeyValuePair <string, SoundEffectInstance> keyValuePair in SoundEffectInstances)
                        {
                            if (keyValuePair.Key != "BackgroundMusic")
                            {
                                if (keyValuePair.Value.Volume > 0.1f)                       // Move the side scrolling volume controls
                                {
                                    keyValuePair.Value.Volume -= 0.1f;

                                    if (!movedSlider)
                                    {
                                        foreach (Option menuOption in OptionElements)
                                        {
                                            if (menuOption.Name == "SFXSlider")
                                            {
                                                menuOption.CreateRectangle(                 // Adjust the volume
                                                    (int)(optionMenuDictionary["SFXSlider"].X - 10),
                                                    (int)(optionMenuDictionary["SFXSlider"].Y)
                                                    );

                                                optionMenuDictionary["SFXSlider"] = new Vector2(
                                                    (int)(optionMenuDictionary["SFXSlider"].X - 10),
                                                    (int)(optionMenuDictionary["SFXSlider"].Y)
                                                    );

                                                SoundEffectInstances["MenuMove"].Play();
                                            }
                                        }

                                        movedSlider = true;
                                    }
                                }
                                else
                                {
                                    SoundEffectInstances["Error"].Play();
                                }
                            }
                        }

                        break;
                    }
                }
            }
        }