protected override void OnKeyDown(KeyEventArgs args) { // UnityEngine.Debug.Log(args.Key); Desktop root = Desktop; if (root == null) { return; } BlinkTime = 0; DoBlink = 1; if (ReadOnly) { return; } if (args.Key == Keys.HOME && !ReadOnly) // home { if (Gui.ShiftPressed) { if (SelectStart == Caret) { Caret = SelectStart = 0; } else if (SelectEnd == Caret) { if (0 < SelectStart) { SelectEnd = SelectStart; SelectStart = 0; Caret = 0; } else { Caret = SelectEnd = 0; } } } else { Caret = 0; SelectStart = SelectEnd = Caret; } } else if (args.Key == Keys.END && !ReadOnly) // end { if (Gui.ShiftPressed) { if (SelectEnd == Caret) { Caret = SelectEnd = Text.Length; } else if (SelectStart == Caret) { if (Text.Length > SelectEnd) { SelectStart = SelectEnd; SelectEnd = Text.Length; Caret = Text.Length; } else { Caret = SelectStart = Text.Length; } } } else { Caret = Text.Length; SelectStart = SelectEnd = Caret; } } else if (args.Key == Keys.RIGHTARROW && !ReadOnly) // right arrow { HandleRightArrow(); } else if (args.Key == Keys.LEFTARROW && !ReadOnly) // left arrow { HandleLeftArrow(); } else if (args.Key == Keys.BACKSPACE && !ReadOnly) // backspace { if (IsSelection) { int start = Math.Min(SelectStart, SelectEnd); int end = Math.Max(SelectStart, SelectEnd); Text = Text.Remove(start, end - start); Caret = start; Offset = 0; } else { if (Caret > 0) { Text = Text.Remove(Caret - 1, 1); if (Caret > 0) { Caret--; } } } SelectStart = SelectEnd = Caret; } else if (args.Key == Keys.DELETE && !ReadOnly) // delete { if (IsSelection) { int start = Math.Min(SelectStart, SelectEnd); int end = Math.Max(SelectStart, SelectEnd); Text = Text.Remove(start, end - start); Caret = start; Offset = 0; } else { if (Text.Length > Caret) { Text = Text.Remove(Caret, 1); } } SelectStart = SelectEnd = Caret; } else if (args.Key == Keys.RETURN || args.Key == Keys.NUMPADENTER) // return/enter { LostFocus -= TextBox_LostFocus; root.FocusedControl = null; Caret = 0; SelectStart = SelectEnd = Caret; LostFocus += TextBox_LostFocus; if (TextCommit != null) { TextCommit(this, null); } } else if (args.Key == Keys.ESCAPE) { LostFocus -= TextBox_LostFocus; Text = SavedText; root.FocusedControl = null; Caret = 0; HasFocus = false; SelectStart = SelectEnd = Caret; LostFocus += TextBox_LostFocus; if (TextCancel != null) { TextCancel(this, null); } } else { if (Gui.CtrlPressed && !Gui.AltPressed) { if (args.Key == Keys.A) // select all { SelectStart = 0; SelectEnd = Text.Length; Caret = Text.Length; } else if (args.Key == Keys.C) // copy { if (IsSelection) { Gui.SetClipboard(Selection); } } else if (args.Key == Keys.X) // copy { if (IsSelection) { Gui.SetClipboard(Selection); int start = Math.Min(SelectStart, SelectEnd); int end = Math.Max(SelectStart, SelectEnd); Text = Text.Remove(start, end - start); Caret = start; Offset = 0; } } else if (args.Key == Keys.V && !ReadOnly) // paste { string paste = Gui.GetClipboard(); if (!string.IsNullOrEmpty(paste)) { if (IsSelection) { int start = Math.Min(SelectStart, SelectEnd); int end = Math.Max(SelectStart, SelectEnd); Text = Text.Remove(start, end - start); Caret = start; } Text = Text.Insert(Caret, paste.ToString()); if (Caret < Text.Length) { Caret += paste.Length; } SelectStart = SelectEnd = Caret; } } } else { if (args.Key != Keys.TAB) { if (args.Char.HasValue) { bool valid = true; char c = args.Char.Value; if (Mode == TextBoxMode.Numeric) { valid = char.IsNumber(c) || char.IsDigit(c) || (c.ToString() == ".") || (c.ToString() == ","); } if (valid) { if (IsSelection) { int start = Math.Min(SelectStart, SelectEnd); int end = Math.Max(SelectStart, SelectEnd); Text = Text.Remove(start, end - start); Caret = start; } Text = Text.Insert(Caret, c.ToString()); if (Caret < Text.Length) { Caret++; } SelectStart = SelectEnd = Caret; } } } } } }
protected override void OnKeyDown(KeyEventArgs args) { Desktop root = Desktop; if (root == null) { return; } BlinkTime = 0; DoBlink = 1; if (args.Key == Keys.HOME && !ReadOnly) // home { if (Gui.ShiftPressed) { if (_selectStart == Caret) { Caret = _selectStart = 0; } else if (_selectEnd == Caret) { if (0 < _selectStart) { _selectEnd = _selectStart; _selectStart = 0; Caret = 0; } else { Caret = _selectEnd = 0; } } } else { Caret = 0; _selectStart = _selectEnd = Caret; } } else if (args.Key == Keys.END && !ReadOnly) // end { if (Gui.ShiftPressed) { if (_selectEnd == Caret) { Caret = _selectEnd = _text.Length; } else if (_selectStart == Caret) { if (_text.Length > _selectEnd) { _selectStart = _selectEnd; _selectEnd = _text.Length; Caret = _text.Length; } else { Caret = _selectStart = _text.Length; } } } else { Caret = _text.Length; _selectStart = _selectEnd = Caret; } } else if (args.Key == Keys.RIGHTARROW && !ReadOnly) // right arrow { HandleRightArrow(); } else if (args.Key == Keys.LEFTARROW && !ReadOnly) // left arrow { HandleLeftArrow(); } else if (args.Key == Keys.UPARROW && !ReadOnly) // up arrow { HandleUpArrow(); } else if (args.Key == Keys.DOWNARROW && !ReadOnly) // down arrow { HandleDownArrow(); } else if (args.Key == Keys.BACKSPACE && !ReadOnly) // backspace { //if (Caret > 0) //{ // HandleLeftArrow(); // if (_text.Length > Caret) // SetText(_text.Remove(Caret, 1)); //} if (IsSelection) { int start = SelectionStart; int end = SelectionEnd; SetText(_text.Remove(start, end - start)); Caret = start; } else { if (Caret > 0) { SetText(_text.Remove(Caret - 1, 1)); if (Caret > 0) { Caret--; } } } _selectStart = _selectEnd = Caret; } else if (args.Key == Keys.DELETE && !ReadOnly) // delete { //if (_text.Length > Caret) // SetText(_text.Remove(Caret, 1)); if (IsSelection) { int start = SelectionStart; int end = SelectionEnd; SetText(_text.Remove(start, end - start)); Caret = start; } else { if (_text.Length > Caret) { SetText(_text.Remove(Caret, 1)); } } _selectStart = _selectEnd = Caret; } else if (args.Key == Keys.RETURN || args.Key == Keys.NUMPADENTER) // return/enter { SetText(_text.Insert(Caret, "\n")); Caret++; } else if (args.Key == Keys.ESCAPE) { LostFocus -= TextBox_LostFocus; SetText(SavedText); Caret = 0; root.FocusedControl = null; LostFocus += TextBox_LostFocus; if (TextCancel != null) { TextCancel(this, null); } } else { if (Gui.CtrlPressed && !Gui.AltPressed) { if (args.Key == Keys.A) // select all { _selectStart = 0; _selectEnd = _text.Length; Caret = _text.Length; } else if (args.Key == Keys.C) // copy { if (IsSelection) { Gui.SetClipboard(Selection.Replace("\n", "\r\n")); } } else if (args.Key == Keys.X) // copy { if (IsSelection) { Gui.SetClipboard(Selection); int start = SelectionStart; int end = SelectionEnd; SetText(_text.Remove(start, end - start)); //Caret = start; _selectStart = _selectEnd = Caret = start; } } else if (args.Key == Keys.V && !ReadOnly) // pasteb { string paste = Gui.GetClipboard().Replace("\r\n", "\n"); if (!string.IsNullOrEmpty(paste)) { if (IsSelection) { int start = SelectionStart; int end = SelectionEnd; SetText(_text.Remove(start, end - start)); Caret = start; } SetText(_text.Insert(Caret, paste.ToString())); if (Caret < _text.Length) { Caret += paste.Length; } _selectStart = _selectEnd = Caret; } } } else { if (args.Key != Keys.TAB) { if (args.Char.HasValue) { bool valid = true; char c = args.Char.Value; if (valid) { if (IsSelection) { int start = SelectionStart; int end = SelectionEnd; SetText(_text.Remove(start, end - start)); Caret = start; } SetText(_text.Insert(Caret, c.ToString())); if (Caret < _text.Length) { Caret++; } _selectStart = _selectEnd = Caret; } } } } } }