/// <summary> /// Update cursor and keyboard events /// </summary> private void UpdateCursorAndKeyboardEvents() { if (!Visible) { return; } MouseEvent mouseState = MouseHandler.GetState(); KeyboardState kbState = KeyboardHandler.GetState(); var mouseSize = new Vector2(5); var mouseRect = new Rectangle((int)mouseState.X, (int)mouseState.Y, (int)mouseSize.X, (int)mouseSize.Y); var thisRect = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y); //First check position if (mouseRect.Intersects(thisRect)) { //Second, check buttons //Left if (LastMouseCheck.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed) { OnMouseDown?.Invoke(this, mouseState.Position, MouseButtons.Left); } if (LastMouseCheck.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released) { OnMouseUp?.Invoke(this, mouseState.Position, MouseButtons.Left); OnMouseClick?.Invoke(this, mouseState.Position, MouseButtons.Left); } //Right if (LastMouseCheck.RightButton == ButtonState.Released && mouseState.RightButton == ButtonState.Pressed) { OnMouseDown?.Invoke(this, mouseState.Position, MouseButtons.Right); } if (LastMouseCheck.RightButton == ButtonState.Pressed && mouseState.RightButton == ButtonState.Released) { OnMouseUp?.Invoke(this, mouseState.Position, MouseButtons.Right); OnMouseClick?.Invoke(this, mouseState.Position, MouseButtons.Right); } //Middle if (LastMouseCheck.MiddleButton == ButtonState.Released && mouseState.MiddleButton == ButtonState.Pressed) { OnMouseDown?.Invoke(this, mouseState.Position, MouseButtons.Middle); } if (LastMouseCheck.MiddleButton == ButtonState.Pressed && mouseState.MiddleButton == ButtonState.Released) { OnMouseUp?.Invoke(this, mouseState.Position, MouseButtons.Middle); OnMouseClick?.Invoke(this, mouseState.Position, MouseButtons.Middle); } //Check move if (LastMouseCheck.Position != mouseState.Position) { OnMouseMove?.Invoke(this, mouseState.Position, MouseButtons.None); } //Hook Keyboard if (kbState.GetPressedKeys().Count() > 0 && LastKeyboardState.GetPressedKeys().Count() > 0) { //check pressed keys List <Keys> pressedKeys = kbState.GetPressedKeys().Except(LastKeyboardState.GetPressedKeys()).ToList(); foreach (Keys p in pressedKeys) { OnKeyPress?.Invoke(this, p, kbState); } //check released keys List <Keys> releasedKeys = LastKeyboardState.GetPressedKeys().Except(kbState.GetPressedKeys()).ToList(); foreach (Keys r in releasedKeys) { OnKeyUp?.Invoke(this, r, kbState); OnKeyPress?.Invoke(this, r, kbState); } } } else { // Leave == Release //Left } LastMouseCheck = mouseState; LastKeyboardState = kbState; }