Skip to content

Instantly share code, notes, and snippets.

@SuperJMN
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SuperJMN/209f10e089d3ce304db1 to your computer and use it in GitHub Desktop.
Save SuperJMN/209f10e089d3ce304db1 to your computer and use it in GitHub Desktop.
Comparison
protected override void OnKeyDown(KeyEventArgs e)
{
var modifiers = e.Device.Modifiers;
var key = e.Key;
if (IsNavigational(key))
{
var shouldSelectAlong = modifiers.HasFlag(ModifierKeys.Shift);
var isWordJumpEnabled = modifiers.HasFlag(ModifierKeys.Control);
this.MoveCaret(TranslateToDirection(key, modifiers), shouldSelectAlong, isWordJumpEnabled);
}
else if (IsTextInput(e, modifiers))
{
this.InsertText(e.Text);
}
else
{
this.TryPerformSpecialInput(key, modifiers);
}
e.Handled = true;
}
protected override void OnKeyDown(KeyEventArgs e)
{
string text = this.Text ?? string.Empty;
int caretIndex = this.CaretIndex;
bool movement = false;
bool textEntered = false;
var modifiers = e.Device.Modifiers;
switch (e.Key)
{
case Key.A:
if (modifiers == ModifierKeys.Control)
{
this.SelectionStart = 0;
this.SelectionEnd = this.Text.Length;
}
break;
case Key.Left:
this.MoveHorizontal(-1, modifiers);
movement = true;
break;
case Key.Right:
this.MoveHorizontal(1, modifiers);
movement = true;
break;
case Key.Up:
this.MoveVertical(-1, modifiers);
movement = true;
break;
case Key.Down:
this.MoveVertical(1, modifiers);
movement = true;
break;
case Key.Home:
this.MoveHome(modifiers);
movement = true;
break;
case Key.End:
this.MoveEnd(modifiers);
movement = true;
break;
case Key.Back:
if (!this.DeleteSelection() && this.CaretIndex > 0)
{
this.Text = text.Substring(0, caretIndex - 1) + text.Substring(caretIndex);
--this.CaretIndex;
}
break;
case Key.Delete:
if (!this.DeleteSelection() && caretIndex < text.Length)
{
this.Text = text.Substring(0, caretIndex) + text.Substring(caretIndex + 1);
}
break;
case Key.Enter:
if (this.AcceptsReturn)
{
goto default;
}
break;
case Key.Tab:
if (this.AcceptsTab)
{
goto default;
}
break;
default:
if (!string.IsNullOrEmpty(e.Text))
{
this.DeleteSelection();
caretIndex = this.CaretIndex;
text = this.Text;
this.Text = text.Substring(0, caretIndex) + e.Text + text.Substring(caretIndex);
++this.CaretIndex;
textEntered = true;
}
break;
}
if (movement && ((modifiers & ModifierKeys.Shift) != 0))
{
this.SelectionEnd = this.CaretIndex;
}
else if (movement || textEntered)
{
this.SelectionStart = this.SelectionEnd = this.CaretIndex;
}
e.Handled = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment