/// <summary> /// Occurs when a key is released. /// </summary> /// <param name="e"></param> protected virtual void OnKeyUp(KeyControlEventArgs e) { e.SourceControl = this; EventHandler<KeyControlEventArgs> handler = KeyUp; if (handler != null) { handler(this, e); } if (this.ShortcutKey.HasValue && e.SourceKey.HasFlag(this.ShortcutKey.Value)) { OnClick(e); } foreach (var control in this.Children) { control.OnKeyUp(e); } }
/// <summary> /// Occurs when a key is depressed. /// </summary> /// <param name="e"></param> protected override void OnKeyDown(KeyControlEventArgs e) { if (this.HasFocus) { bool upper = false; //TODO: handle caps lock. if (e.IsShiftDepressed) { upper = !upper; } if (e.SourceKey == Keys.Back) { if (this.Text.Length > 0) { this.Text = this.Text.Remove(this.CaretPosition, 1); this.CaretPosition -= 1; } } else if (e.SourceKey == Keys.A) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "A"); } else { this.Text = this.Text.Insert(this.CaretPosition, "a"); } } else if (e.SourceKey == Keys.B) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "B"); } else { this.Text = this.Text.Insert(this.CaretPosition, "b"); } } else if (e.SourceKey == Keys.C) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "C"); } else { this.Text = this.Text.Insert(this.CaretPosition, "c"); } } else if (e.SourceKey == Keys.D) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "D"); } else { this.Text = this.Text.Insert(this.CaretPosition, "d"); } } else if (e.SourceKey == Keys.E) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "E"); } else { this.Text = this.Text.Insert(this.CaretPosition, "e"); } } else if (e.SourceKey == Keys.F) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "F"); } else { this.Text = this.Text.Insert(this.CaretPosition, "f"); } } else if (e.SourceKey == Keys.G) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "G"); } else { this.Text = this.Text.Insert(this.CaretPosition, "g"); } } else if (e.SourceKey == Keys.H) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "H"); } else { this.Text = this.Text.Insert(this.CaretPosition, "h"); } } else if (e.SourceKey == Keys.I) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "I"); } else { this.Text = this.Text.Insert(this.CaretPosition, "i"); } } else if (e.SourceKey == Keys.J) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "J"); } else { this.Text = this.Text.Insert(this.CaretPosition, "j"); } } else if (e.SourceKey == Keys.K) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "K"); } else { this.Text = this.Text.Insert(this.CaretPosition, "k"); } } else if (e.SourceKey == Keys.L) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "L"); } else { this.Text = this.Text.Insert(this.CaretPosition, "l"); } } else if (e.SourceKey == Keys.M) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "M"); } else { this.Text = this.Text.Insert(this.CaretPosition, "m"); } } else if (e.SourceKey == Keys.N) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "N"); } else { this.Text = this.Text.Insert(this.CaretPosition, "n"); } } else if (e.SourceKey == Keys.O) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "O"); } else { this.Text = this.Text.Insert(this.CaretPosition, "o"); } } else if (e.SourceKey == Keys.P) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "P"); } else { this.Text = this.Text.Insert(this.CaretPosition, "p"); } } else if (e.SourceKey == Keys.Q) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "Q"); } else { this.Text = this.Text.Insert(this.CaretPosition, "q"); } } else if (e.SourceKey == Keys.R) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "R"); } else { this.Text = this.Text.Insert(this.CaretPosition, "r"); } } else if (e.SourceKey == Keys.S) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "S"); } else { this.Text = this.Text.Insert(this.CaretPosition, "s"); } } else if (e.SourceKey == Keys.T) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "T"); } else { this.Text = this.Text.Insert(this.CaretPosition, "t"); } } else if (e.SourceKey == Keys.U) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "U"); } else { this.Text = this.Text.Insert(this.CaretPosition, "u"); } } else if (e.SourceKey == Keys.V) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "V"); } else { this.Text = this.Text.Insert(this.CaretPosition, "v"); } } else if (e.SourceKey == Keys.W) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "W"); } else { this.Text = this.Text.Insert(this.CaretPosition, "w"); } } else if (e.SourceKey == Keys.X) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "X"); } else { this.Text = this.Text.Insert(this.CaretPosition, "x"); } } else if (e.SourceKey == Keys.Y) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "Y"); } else { this.Text = this.Text.Insert(this.CaretPosition, "y"); } } else if (e.SourceKey == Keys.Z) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "Z"); } else { this.Text = this.Text.Insert(this.CaretPosition, "z"); } } else if (e.SourceKey == Keys.Space) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, " "); } else { this.Text = this.Text.Insert(this.CaretPosition, " "); } } else if ((e.SourceKey == Keys.D0) || (e.SourceKey == Keys.NumPad0)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, ")"); } else { this.Text = this.Text.Insert(this.CaretPosition, "0"); } } else if ((e.SourceKey == Keys.D1) || (e.SourceKey == Keys.NumPad1)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "!"); } else { this.Text = this.Text.Insert(this.CaretPosition, "1"); } } else if ((e.SourceKey == Keys.D2) || (e.SourceKey == Keys.NumPad2)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "@"); } else { this.Text = this.Text.Insert(this.CaretPosition, "2"); } } else if ((e.SourceKey == Keys.D3) || (e.SourceKey == Keys.NumPad3)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "#"); } else { this.Text = this.Text.Insert(this.CaretPosition, "3"); } } else if ((e.SourceKey == Keys.D4) || (e.SourceKey == Keys.NumPad4)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "$"); } else { this.Text = this.Text.Insert(this.CaretPosition, "4"); } } else if ((e.SourceKey == Keys.D5) || (e.SourceKey == Keys.NumPad5)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "%"); } else { this.Text = this.Text.Insert(this.CaretPosition, "5"); } } else if ((e.SourceKey == Keys.D6) || (e.SourceKey == Keys.NumPad6)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "^"); } else { this.Text = this.Text.Insert(this.CaretPosition, "6"); } } else if ((e.SourceKey == Keys.D7) || (e.SourceKey == Keys.NumPad7)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "&"); } else { this.Text = this.Text.Insert(this.CaretPosition, "7"); } } else if ((e.SourceKey == Keys.D8) || (e.SourceKey == Keys.NumPad8)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "*"); } else { this.Text = this.Text.Insert(this.CaretPosition, "8"); } } else if ((e.SourceKey == Keys.D9) || (e.SourceKey == Keys.NumPad9)) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "("); } else { this.Text = this.Text.Insert(this.CaretPosition, "9"); } } else if (e.SourceKey == Keys.OemMinus) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "_"); } else { this.Text = this.Text.Insert(this.CaretPosition, "-"); } } else if (e.SourceKey == Keys.OemPlus) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "+"); } else { this.Text = this.Text.Insert(this.CaretPosition, "="); } } else if (e.SourceKey == Keys.OemQuestion) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "?"); } else { this.Text = this.Text.Insert(this.CaretPosition, "/"); } } else if (e.SourceKey == Keys.OemQuotes) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "\""); } else { this.Text = this.Text.Insert(this.CaretPosition, "'"); } } else if (e.SourceKey == Keys.OemComma) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "<"); } else { this.Text = this.Text.Insert(this.CaretPosition, ","); } } else if (e.SourceKey == Keys.OemPeriod) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, ">"); } else { this.Text = this.Text.Insert(this.CaretPosition, "."); } } else if (e.SourceKey == Keys.OemTilde) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "~"); } else { this.Text = this.Text.Insert(this.CaretPosition, "`"); } } else if (e.SourceKey == Keys.OemOpenBrackets) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "{"); } else { this.Text = this.Text.Insert(this.CaretPosition, "["); } } else if (e.SourceKey == Keys.OemCloseBrackets) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "}"); } else { this.Text = this.Text.Insert(this.CaretPosition, "]"); } } else if (e.SourceKey == Keys.OemPipe) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, "|"); } else { this.Text = this.Text.Insert(this.CaretPosition, "\\"); } } else if (e.SourceKey == Keys.OemSemicolon) { this.CaretPosition += 1; if (upper) { this.Text = this.Text.Insert(this.CaretPosition, ":"); } else { this.Text = this.Text.Insert(this.CaretPosition, ";"); } } } base.OnKeyDown(e); }
/// <summary> /// Occurs when a key is depressed. /// </summary> /// <param name="e"></param> protected virtual void OnKeyDown(KeyControlEventArgs e) { e.SourceControl = this; EventHandler<KeyControlEventArgs> handler = KeyDown; if (handler != null) { handler(this, e); } //if (e.SourceKey.HasFlag(this.ShortcutKey)) //{ // OnClick(e); //} foreach (var control in this.Children) { control.OnKeyDown(e); } }
/// <summary> /// Raises events for controls under the Gui manager. /// This should be called from the Game Update method. /// </summary> public void Update() { var mouseState = Mouse.GetState(); var keyboardState = Keyboard.GetState(); var gamepadState = GamePad.GetState(PlayerIndex.One); //TODO: design gamepad support. this.MouseArea = new Rectangle(mouseState.X,mouseState.Y,1,1); //used for intersect. //Mouse Down var mouseDownArguments = new MouseControlEventArgs(this); mouseDownArguments.SourceArea = this.MouseArea; if (mouseState.LeftButton == ButtonState.Pressed && mOldMouseState.LeftButton != ButtonState.Pressed) { mouseDownArguments.Left = true; mLeftMouseDownArea = mouseDownArguments.SourceArea; } if (mouseState.MiddleButton == ButtonState.Pressed && mOldMouseState.MiddleButton != ButtonState.Pressed) { mouseDownArguments.Middle = true; mMiddleMouseDownArea = mouseDownArguments.SourceArea; } if (mouseState.RightButton == ButtonState.Pressed && mOldMouseState.RightButton != ButtonState.Pressed) { mouseDownArguments.Right = true; mRightMouseDownArea = mouseDownArguments.SourceArea; } if (mouseDownArguments.Left || mouseDownArguments.Middle || mouseDownArguments.Right) { base.OnMouseDown(mouseDownArguments); } //Mouse Up var mouseUpArguments = new MouseControlEventArgs(this); mouseUpArguments.SourceArea = this.MouseArea; if (mouseState.LeftButton == ButtonState.Released && mOldMouseState.LeftButton != ButtonState.Released) { mouseUpArguments.Left = true; mouseUpArguments.LeftArea = mLeftMouseDownArea; } if (mouseState.MiddleButton == ButtonState.Released && mOldMouseState.MiddleButton != ButtonState.Released) { mouseUpArguments.Middle = true; mouseUpArguments.MiddleArea = mMiddleMouseDownArea; } if (mouseState.RightButton == ButtonState.Released && mOldMouseState.RightButton != ButtonState.Released) { mouseUpArguments.Right = true; mouseUpArguments.RightArea = mRightMouseDownArea; } if (mouseUpArguments.Left || mouseUpArguments.Middle || mouseUpArguments.Right) { base.OnMouseUp(mouseUpArguments); } //Key Down bool isShiftDepressed = keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift); bool isAltDepressed = keyboardState.IsKeyDown(Keys.LeftAlt) || keyboardState.IsKeyDown(Keys.RightAlt); bool isControlDepressed = keyboardState.IsKeyDown(Keys.LeftControl) || keyboardState.IsKeyDown(Keys.RightControl); foreach (var pressedKey in keyboardState.GetPressedKeys()) { if (this.mOldKeyboardState.IsKeyUp(pressedKey)) { var keyDownArguments = new KeyControlEventArgs(this); keyDownArguments.IsShiftDepressed = isShiftDepressed; keyDownArguments.IsAltDepressed = isAltDepressed; keyDownArguments.IsControlDepressed = isControlDepressed; keyDownArguments.SourceKey = pressedKey; base.OnKeyDown(keyDownArguments); } } //Key Up foreach (var pressedKey in this.mOldKeyboardState.GetPressedKeys()) { if (keyboardState.IsKeyUp(pressedKey)) { var keyUpArguments = new KeyControlEventArgs(this); keyUpArguments.SourceKey = pressedKey; base.OnKeyUp(keyUpArguments); } } //change old state so events don't repeat. mOldMouseState = mouseState; mOldKeyboardState = keyboardState; }