Example #1
0
 public void GetKey()
 {
     if (Input.GetKey(_keyCode))
     {
         OnKeyHeld.Invoke(true);
     }
     else
     {
         OnKeyHeld.Invoke(false);
     }
 }
Example #2
0
        public void Update()
        {
            //Update keyboard states
            PrevKeyboardState    = CurrentKeyboardState;
            CurrentKeyboardState = Keyboard.GetState();
            //Update mouse states
            PrevMouseState    = CurrentMouseState;
            CurrentMouseState = Mouse.GetState();

            foreach (var key in KeySet)
            {
                var isDown  = CurrentKeyboardState.IsKeyDown(key);
                var wasDown = PrevKeyboardState.IsKeyDown(key);

                if (isDown)//Key is down
                {
                    //Key is currently held
                    OnKeyHeld?.Invoke(this, new KeyEventArgs(key));

                    if (!wasDown)//Key has just been pressed
                    {
                        OnKeyDown?.Invoke(this, new KeyEventArgs(key));
                    }
                }
                else  //Key is not down
                {
                    if (wasDown)//Key has just been released
                    {
                        OnKeyUp?.Invoke(this, new KeyEventArgs(key));
                    }
                }
            }

            foreach (var button in ButtonSet)
            {
                bool isDown  = false;
                bool wasDown = false;

                switch (button)
                {
                case MouseButton.left:
                {
                    isDown  = CurrentMouseState.LeftButton == ButtonState.Pressed;
                    wasDown = PrevMouseState.LeftButton == ButtonState.Pressed;
                }
                break;

                case MouseButton.right:
                {
                    isDown  = CurrentMouseState.RightButton == ButtonState.Pressed;
                    wasDown = PrevMouseState.RightButton == ButtonState.Pressed;
                }
                break;

                default:
                    break;
                }

                if (isDown)//Button is down
                {
                    OnMouseButtonHeld?.Invoke(this, new MouseEventArgs(button, CurrentMouseState));

                    if (!wasDown)//Button has just been pressed
                    {
                        OnMouseButtonDown?.Invoke(this, new MouseEventArgs(button, CurrentMouseState));
                    }
                }
                else
                {                //Button is not down
                    if (wasDown) //Button has just been released
                    {
                        OnMouseButtonUp?.Invoke(this, new MouseEventArgs(button, CurrentMouseState));
                    }
                }
            }
        }