private void KeyBox_KeyDown(object sender, KeyEventArgs e) { //Fix the position of the scrollviewer if necessary: var rect = KeyBox.CaretPosition.GetCharacterRect(LogicalDirection.Forward); if (rect.X < 0) { KeyBox.ScrollToHorizontalOffset(KeyBox.HorizontalOffset + rect.X - 5); } if (rect.X + rect.Width > KeyBox.ActualWidth) { KeyBox.ScrollToHorizontalOffset(KeyBox.HorizontalOffset + (rect.X + rect.Width - KeyBox.ActualWidth) + 5); } }
private void KeyBox_PreviewKeyDown(object sender, KeyEventArgs e) { var key = KeyManager.GetKey(); switch (e.Key) { case Key.Space: e.Handled = true; HandleInput(" "); break; case Key.Back: case Key.Delete: e.Handled = true; var caretIndex = GetKeyOffset(KeyBox.CaretPosition); if (e.Key == Key.Back) { if (caretIndex == 0) { break; } caretIndex--; } else if (e.Key == Key.Delete && caretIndex == key.Length) { break; } ReplaceCharInKey(key, GetPossibleCharactersAtKeyEnd(key.Substring(0, caretIndex)), '*', caretIndex); SetKeyOffset(caretIndex); break; case Key.Home: KeyBox.ScrollToHome(); break; case Key.End: var x = KeyBox.Document.ContentEnd.GetCharacterRect(LogicalDirection.Forward).X; KeyBox.ScrollToHorizontalOffset(x + KeyBox.HorizontalOffset - KeyBox.ActualWidth + 5); break; } }