/// Here we handle the keyboard to check for the arrow keys. KeyboardData contains basically the same /// information as TCODKey. /// /// All of the On* methods of the base classes are message handlers - that is, they are called when the /// framework sends them a message. The base classes typically perform all the grunge work of handling /// the messages (including triggering any events), so to add custom message handling we just override /// the relavent On* method. An alternative to overriding is to hook into the events, but it is usually /// preferrable to using an override instead of events (for efficiency and code cleanliness reasons, among /// others). protected override void OnKeyPressed(KeyboardData keyData) { base.OnKeyPressed(keyData); switch (keyData.KeyCode) { case libtcod.TCODKeyCode.Left: if (currentPos.X > 0) currentPos = currentPos.Shift(-1, 0); break; case libtcod.TCODKeyCode.Right: if (currentPos.X < Application.ScreenSize.Width-1) currentPos = currentPos.Shift(1, 0); break; case libtcod.TCODKeyCode.Down: if (currentPos.Y < Application.ScreenSize.Height-1) currentPos = currentPos.Shift(0, 1); break; case libtcod.TCODKeyCode.Up: if (currentPos.Y > 0) currentPos = currentPos.Shift(0, -1); break; } }
// ///////////////////////////////////////////////////////////////////////////////// /// Base method propagates messages to children controls and managers. Override to /// add custom handling. protected internal override void OnKeyReleased(KeyboardData keyData) { base.OnKeyReleased(keyData); foreach (Manager m in managerList) { m.OnKeyReleased(keyData); } if (CurrentKeyboardFocus != null) { CurrentKeyboardFocus.OnKeyReleased(keyData); } }
/// <summary> /// It should be noted that Managers always receive input messages, unlike controls. A control /// needs to have the current keyboard focus before it can receive keyboard messages, for example. All /// Manager objects added to the current Window will receive all mouse and keyboard messages. /// </summary> /// <param name="keyData"></param> protected override void OnKeyPressed(KeyboardData keyData) { base.OnKeyPressed(keyData); switch (keyData.KeyCode) { case libtcod.TCODKeyCode.Up: mapView.MovePlayer(0, -1); break; case libtcod.TCODKeyCode.Down: mapView.MovePlayer(0, 1); break; case libtcod.TCODKeyCode.Right: mapView.MovePlayer(1, 0); break; case libtcod.TCODKeyCode.Left: mapView.MovePlayer(-1, 0); break; } }
// ///////////////////////////////////////////////////////////////////////////////// /// <summary> /// Base method handles keyboard input for the entry. Override to add custom /// behavior. /// </summary> internal protected override void OnKeyPressed(KeyboardData keyData) { base.OnKeyPressed(keyData); if (keyData.Character != 0 && ValidateCharacter(keyData.Character)) { if (waitingToOverwrite) { TextInput = keyData.Character.ToString(); CursorPos = 1; waitingToOverwrite = false; } else if (TextInput.Length < MaximumCharacters) { TextInput += keyData.Character; CursorPos++; } } else if (keyData.KeyCode == TCODKeyCode.Backspace && TextInput.Length > 0) { TextInput = TextInput.Substring(0, TextInput.Length - 1); CursorPos--; } else if (keyData.KeyCode == TCODKeyCode.Enter) { waitingToCommitText = true; ParentWindow.ReleaseKeyboard(this); } else if (keyData.KeyCode == TCODKeyCode.Escape) { TextInput = CurrentText; waitingToCommitText = true; ParentWindow.ReleaseKeyboard(this); } }
// ///////////////////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////////////////// /// <summary> /// Called when the user has released a previously pressed key. Override to add /// key release handling code. /// </summary> /// <param name="keyData"></param> protected internal virtual void OnKeyReleased(KeyboardData keyData) { if (KeyReleased != null) { KeyReleased(this, new KeyboardEventArgs(keyData)); } }
/// <summary> /// Construct a KeyboardEventArgs given the KeyboardData /// </summary> /// <param name="keyboardData"></param> public KeyboardEventArgs(KeyboardData keyboardData) { if (keyboardData == null) { throw new ArgumentNullException("keyboardData"); } this.keyboardData = keyboardData; }