protected override bool OnKeyDown(InputState state, KeyDownEventArgs args) { if (!HasFocus) { return(false); } if (textInput?.ImeActive == true) { return(true); } switch (args.Key) { case Key.Tab: return(false); case Key.End: moveSelection(InternalText.Length, state.Keyboard.ShiftPressed); return(true); case Key.Home: moveSelection(-InternalText.Length, state.Keyboard.ShiftPressed); return(true); case Key.Left: { if (!HandleLeftRightArrows) { return(false); } if (selectionEnd == 0) { //we only clear if you aren't holding shift if (!state.Keyboard.ShiftPressed) { resetSelection(); } return(true); } int amount = 1; if (state.Keyboard.ControlPressed) { int lastSpace = InternalText.LastIndexOf(' ', Math.Max(0, selectionEnd - 2)); if (lastSpace >= 0) { //if you have something selected and shift is not held down //A selection reset is required to select a word inside the current selection if (!state.Keyboard.ShiftPressed) { resetSelection(); } amount = selectionEnd - lastSpace - 1; } else { amount = selectionEnd; } } moveSelection(-amount, state.Keyboard.ShiftPressed); return(true); } case Key.Right: { if (!HandleLeftRightArrows) { return(false); } if (selectionEnd == InternalText.Length) { if (!state.Keyboard.ShiftPressed) { resetSelection(); } return(true); } int amount = 1; if (state.Keyboard.ControlPressed) { int nextSpace = InternalText.IndexOf(' ', selectionEnd + 1); if (nextSpace >= 0) { if (!state.Keyboard.ShiftPressed) { resetSelection(); } amount = nextSpace - selectionEnd; } else { amount = InternalText.Length - selectionEnd; } } moveSelection(amount, state.Keyboard.ShiftPressed); return(true); } case Key.Enter: selectionStart = selectionEnd = 0; TriggerFocusLost(state); return(true); case Key.Delete: if (selectionLength == 0) { if (InternalText.Length == selectionStart) { return(true); } if (state.Keyboard.ControlPressed) { int spacePos = selectionStart; while (InternalText[spacePos] == ' ' && spacePos < InternalText.Length) { spacePos++; } spacePos = MathHelper.Clamp(InternalText.IndexOf(' ', spacePos), 0, InternalText.Length); selectionEnd = spacePos; if (selectionStart == 0 && spacePos == 0) { selectionEnd = InternalText.Length; } if (selectionLength == 0) { return(true); } } else { //we're deleting in front of the cursor, so move the cursor forward once first selectionStart = selectionEnd = selectionStart + 1; } } removeCharacterOrSelection(); return(true); case Key.Back: if (selectionLength == 0 && state.Keyboard.ControlPressed) { int spacePos = selectionLeft >= 2 ? Math.Max(0, InternalText.LastIndexOf(' ', selectionLeft - 2) + 1) : 0; selectionStart = spacePos; } removeCharacterOrSelection(); return(true); } if (state.Keyboard.ControlPressed) { //handling of function keys switch (args.Key) { case Key.A: selectionStart = 0; selectionEnd = InternalText.Length; cursorAndLayout.Invalidate(); return(true); case Key.C: if (string.IsNullOrEmpty(SelectedText) || !AllowClipboardExport) { return(true); } //System.Windows.Forms.Clipboard.SetText(SelectedText); return(true); case Key.X: if (string.IsNullOrEmpty(SelectedText)) { return(true); } //if (AllowClipboardExport) // System.Windows.Forms.Clipboard.SetText(SelectedText); removeCharacterOrSelection(); return(true); case Key.V: //the text is pasted into the hidden textbox, so we don't need any direct clipboard interaction here. insertString(textInput?.GetPendingText()); return(true); } return(false); } string str = textInput?.GetPendingText(); if (!string.IsNullOrEmpty(str)) { if (state.Keyboard.ShiftPressed) { audio.Sample.Get(@"Keyboard/key-caps")?.Play(); } else { audio.Sample.Get($@"Keyboard/key-press-{RNG.Next(1, 5)}")?.Play(); } insertString(str); } return(true); }