public void TestKeyPressOnFocusAffectingControl() { Control parent = new Control(); Controls.Desktop.WindowControl child1 = new Controls.Desktop.WindowControl(); Controls.Desktop.WindowControl child2 = new Controls.Desktop.WindowControl(); KeyboardTestControl child3 = new KeyboardTestControl(true); parent.Children.Add(child1); parent.Children.Add(child2); parent.Children.Add(child3); KeyboardTestControl child1child1 = new KeyboardTestControl(false); KeyboardTestControl child1child2 = new KeyboardTestControl(false); KeyboardTestControl child2child1 = new KeyboardTestControl(false); child1.Children.Add(child1child1); child1.Children.Add(child1child2); child2.Children.Add(child2child1); parent.ProcessKeyPress(Keys.A, false); Assert.AreEqual(1, child1child1.HeldKeyCount); Assert.AreEqual(1, child1child2.HeldKeyCount); // because child 1 returned false Assert.AreEqual(0, child2child1.HeldKeyCount); // because its parent affects focus Assert.AreEqual(1, child3.HeldKeyCount); // because it doesn't affect focus }
public void TestKeyPressWithActivatedControl() { Control parent = new Control(); parent.Bounds = new UniRectangle(10.0f, 10.0f, 80.0f, 80.0f); KeyboardTestControl child1 = new KeyboardTestControl(true); KeyboardTestControl child2 = new KeyboardTestControl(true); child2.Bounds = new UniRectangle(10.0f, 10.0f, 25.0f, 60.0f); parent.Children.Add(child1); parent.Children.Add(child2); parent.ProcessMouseMove(100.0f, 100.0f, 20.0f, 30.0f); parent.ProcessMousePress(MouseButtons.Left); Assert.AreEqual(0, child1.HeldKeyCount); Assert.AreEqual(0, child2.HeldKeyCount); parent.ProcessKeyPress(Keys.A, false); Assert.AreEqual(0, child1.HeldKeyCount); Assert.AreEqual(1, child2.HeldKeyCount); // Because child 1 was activated parent.ProcessKeyRelease(Keys.A); Assert.AreEqual(0, child1.HeldKeyCount); Assert.AreEqual(0, child2.HeldKeyCount); }
public void TestKeyPressOnFocusAffectingControl() { Control parent = new Control(); Controls.Desktop.WindowControl child1 = new Controls.Desktop.WindowControl(); Controls.Desktop.WindowControl child2 = new Controls.Desktop.WindowControl(); KeyboardTestControl child3 = new KeyboardTestControl(true); parent.Children.Add(child1); parent.Children.Add(child2); parent.Children.Add(child3); KeyboardTestControl child1child1 = new KeyboardTestControl(false); KeyboardTestControl child1child2 = new KeyboardTestControl(false); KeyboardTestControl child2child1 = new KeyboardTestControl(false); child1.Children.Add(child1child1); child1.Children.Add(child1child2); child2.Children.Add(child2child1); parent.ProcessKeyPress(Keys.A, false); Assert.AreEqual(1, child1child1.HeldKeyCount); Assert.AreEqual(1, child1child2.HeldKeyCount); // because child 1 returned false Assert.AreEqual(0, child2child1.HeldKeyCount); // because its parent affects focus Assert.AreEqual(1, child3.HeldKeyCount); // because it doesn't affect focus }
public void TestKeyPressWithActivatedControl() { Control parent = new Control(); parent.Bounds = new UniRectangle(10.0f, 10.0f, 80.0f, 80.0f); KeyboardTestControl child1 = new KeyboardTestControl(true); KeyboardTestControl child2 = new KeyboardTestControl(true); child2.Bounds = new UniRectangle(10.0f, 10.0f, 25.0f, 60.0f); parent.Children.Add(child1); parent.Children.Add(child2); parent.ProcessMouseMove(100.0f, 100.0f, 20.0f, 30.0f); parent.ProcessMousePress(MouseButtons.Left); Assert.AreEqual(0, child1.HeldKeyCount); Assert.AreEqual(0, child2.HeldKeyCount); parent.ProcessKeyPress(Keys.A, false); Assert.AreEqual(0, child1.HeldKeyCount); Assert.AreEqual(1, child2.HeldKeyCount); // Because child 1 was activated parent.ProcessKeyRelease(Keys.A); Assert.AreEqual(0, child1.HeldKeyCount); Assert.AreEqual(0, child2.HeldKeyCount); }
/// <summary>Called when a key on the keyboard has been pressed down</summary> /// <param name="keyCode">Code of the key that was pressed</param> /// <param name="repetition"> /// Whether the key press is due to the user holding down a key /// </param> internal bool ProcessKeyPress(Keys keyCode, bool repetition) { // If there's an activated control (one being held down by the mouse or having // accepted a previous key press), this control will get the key press delivered, // whether it wants to or not. We don't want to track for each key which control // is currently processing it. ;-) if (this.activatedControl != null) { if (!repetition) { ++this.heldKeyCount; } // If one of our children is the activated control, pass on the message if (this.activatedControl != this) { this.activatedControl.ProcessKeyPress(keyCode, repetition); } else // We're the activated control { OnKeyPressed(keyCode); } return(true); // Ignore user code and always accept the key press } // A key has been pressed but no control is activated currently. This means we // have to look for a control which feels responsible for the key press, starting // with ourselves. // Does the user code in our derived class feel responsible for this key? // If so, we're the new activated control and the key has been handled. if (OnKeyPressed(keyCode)) { this.activatedControl = this; ++this.heldKeyCount; return(true); } // Nope, we have to ask our children (and they, potentially recursively, theirs) // to find a control that feels responsible. bool encounteredOrderingControl = false; for (int index = 0; index < this.children.Count; ++index) { Control child = this.children[index]; // We only process one child that has the affectsOrdering field set. This // ensures that key presses will not be delivered to windows sitting behind // another window. Other siblings that are not windows are asked still. if (child.affectsOrdering) { if (encounteredOrderingControl) { continue; } else { encounteredOrderingControl = true; } } // Does this child feel responsible for the key press? if (child.ProcessKeyPress(keyCode, repetition)) { this.activatedControl = child; ++this.heldKeyCount; return(true); } } // Neither we nor any of our children felt responsible for the key. Give up. return(false); }