diff --git a/Terminal.Gui/Application/Application.Keyboard.cs b/Terminal.Gui/Application/Application.Keyboard.cs index a73e7cb86..c9fa96c63 100644 --- a/Terminal.Gui/Application/Application.Keyboard.cs +++ b/Terminal.Gui/Application/Application.Keyboard.cs @@ -5,17 +5,17 @@ public static partial class Application // Keyboard handling { /// /// Called when the user presses a key (by the ). Raises the cancelable - /// event - /// then calls on all top level views. Called before . + /// event, then calls on all top level views, and finally + /// if the key was not handled, invokes any Application-scoped . /// /// Can be used to simulate key press events. - /// + /// /// if the key was handled. - public static bool RaiseKeyDownEvent (Key keyEvent) + public static bool RaiseKeyDownEvent (Key key) { - KeyDown?.Invoke (null, keyEvent); + KeyDown?.Invoke (null, key); - if (keyEvent.Handled) + if (key.Handled) { return true; } @@ -24,7 +24,7 @@ public static partial class Application // Keyboard handling { foreach (Toplevel topLevel in TopLevels.ToList ()) { - if (topLevel.NewKeyDownEvent (keyEvent)) + if (topLevel.NewKeyDownEvent (key)) { return true; } @@ -37,7 +37,7 @@ public static partial class Application // Keyboard handling } else { - if (Top.NewKeyDownEvent (keyEvent)) + if (Top.NewKeyDownEvent (key)) { return true; } @@ -45,7 +45,7 @@ public static partial class Application // Keyboard handling // Invoke any Application-scoped KeyBindings. // The first view that handles the key will stop the loop. - foreach (KeyValuePair binding in KeyBindings.Bindings.Where (b => b.Key == keyEvent.KeyCode)) + foreach (KeyValuePair binding in KeyBindings.Bindings.Where (b => b.Key == key.KeyCode)) { if (binding.Value.BoundView is { }) { @@ -58,7 +58,7 @@ public static partial class Application // Keyboard handling } else { - if (!KeyBindings.TryGet (keyEvent, KeyBindingScope.Application, out KeyBinding appBinding)) + if (!KeyBindings.TryGet (key, KeyBindingScope.Application, out KeyBinding appBinding)) { continue; } @@ -67,7 +67,7 @@ public static partial class Application // Keyboard handling foreach (Command command in appBinding.Commands) { - toReturn = InvokeCommand (command, keyEvent, appBinding); + toReturn = InvokeCommand (command, key, appBinding); } return toReturn ?? true; @@ -76,18 +76,18 @@ public static partial class Application // Keyboard handling return false; - static bool? InvokeCommand (Command command, Key keyEvent, KeyBinding appBinding) + static bool? InvokeCommand (Command command, Key key, KeyBinding appBinding) { if (!CommandImplementations!.ContainsKey (command)) { throw new NotSupportedException ( - @$"A KeyBinding was set up for the command {command} ({keyEvent}) but that command is not supported by Application." + @$"A KeyBinding was set up for the command {command} ({key}) but that command is not supported by Application." ); } if (CommandImplementations.TryGetValue (command, out View.CommandImplementation? implementation)) { - var context = new CommandContext (command, keyEvent, appBinding); // Create the context here + var context = new CommandContext (command, key, appBinding); // Create the context here return implementation (context); } @@ -116,25 +116,25 @@ public static partial class Application // Keyboard handling /// then calls on all top level views. Called after . /// /// Can be used to simulate key release events. - /// + /// /// if the key was handled. - public static bool RaiseKeyUpEvent (Key a) + public static bool RaiseKeyUpEvent (Key key) { if (!IsInitialized) { return true; } - KeyUp?.Invoke (null, a); + KeyUp?.Invoke (null, key); - if (a.Handled) + if (key.Handled) { return true; } foreach (Toplevel topLevel in TopLevels.ToList ()) { - if (topLevel.NewKeyUpEvent (a)) + if (topLevel.NewKeyUpEvent (key)) { return true; } diff --git a/Terminal.Gui/Input/ShortcutHelper.cs b/Terminal.Gui/Input/ShortcutHelper.cs deleted file mode 100644 index d5e776a53..000000000 --- a/Terminal.Gui/Input/ShortcutHelper.cs +++ /dev/null @@ -1,173 +0,0 @@ -using System.Diagnostics; - -namespace Terminal.Gui; - -// TODO: Nuke when #2975 is completed -/// Represents a helper to manipulate shortcut keys used on views. -public class ShortcutHelper -{ - // TODO: Update this to use Key, not KeyCode - private KeyCode shortcut; - - /// This is the global setting that can be used as a global shortcut to invoke the action on the view. - public virtual KeyCode Shortcut - { - get => shortcut; - set - { - if (shortcut != value && (PostShortcutValidation (value) || value is KeyCode.Null)) - { - shortcut = value; - } - } - } - - /// The keystroke combination used in the as string. - public virtual string ShortcutTag => Key.ToString (shortcut, Key.Separator); - - /// Lookup for a on range of keys. - /// The source key. - /// First key in range. - /// Last key in range. - public static bool CheckKeysFlagRange (KeyCode key, KeyCode first, KeyCode last) - { - for (var i = (uint)first; i < (uint)last; i++) - { - if ((key | (KeyCode)i) == key) - { - return true; - } - } - - return false; - } - - /// Allows to retrieve a from a - /// The key as string. - /// The delimiter string. - public static KeyCode GetShortcutFromTag (string tag, Rune delimiter = default) - { - string sCut = tag; - - if (string.IsNullOrEmpty (sCut)) - { - return default (KeyCode); - } - - var key = KeyCode.Null; - - //var hasCtrl = false; - if (delimiter == default (Rune)) - { - delimiter = Key.Separator; - } - - string [] keys = sCut.Split (delimiter.ToString ()); - - for (var i = 0; i < keys.Length; i++) - { - string k = keys [i]; - - if (k == "Ctrl") - { - //hasCtrl = true; - key |= KeyCode.CtrlMask; - } - else if (k == "Shift") - { - key |= KeyCode.ShiftMask; - } - else if (k == "Alt") - { - key |= KeyCode.AltMask; - } - else if (k.StartsWith ("F") && k.Length > 1) - { - int.TryParse (k.Substring (1), out int n); - - for (var j = (uint)KeyCode.F1; j <= (uint)KeyCode.F12; j++) - { - int.TryParse (((KeyCode)j).ToString ().Substring (1), out int f); - - if (f == n) - { - key |= (KeyCode)j; - } - } - } - else - { - key |= (KeyCode)Enum.Parse (typeof (KeyCode), k); - } - } - - return key; - } - - /// Used at key up validation. - /// The key to validate. - /// true if is valid.falseotherwise. - public static bool PostShortcutValidation (KeyCode key) - { - GetKeyToString (key, out KeyCode knm); - - if (CheckKeysFlagRange (key, KeyCode.F1, KeyCode.F12) || ((key & (KeyCode.CtrlMask | KeyCode.ShiftMask | KeyCode.AltMask)) != 0 && knm != KeyCode.Null)) - { - return true; - } - - return false; - } - - /// Used at key down or key press validation. - /// The key to validate. - /// true if is valid.falseotherwise. - public static bool PreShortcutValidation (KeyCode key) - { - if ((key & (KeyCode.CtrlMask | KeyCode.ShiftMask | KeyCode.AltMask)) == 0 - && !CheckKeysFlagRange (key, KeyCode.F1, KeyCode.F12)) - { - return false; - } - - return true; - } - - /// Return key as string. - /// The key to extract. - /// Correspond to the non modifier key. - private static string GetKeyToString (KeyCode key, out KeyCode knm) - { - if (key == KeyCode.Null) - { - knm = KeyCode.Null; - - return ""; - } - - knm = key; - KeyCode mK = key & (KeyCode.AltMask | KeyCode.CtrlMask | KeyCode.ShiftMask); - knm &= ~mK; - - for (var i = (uint)KeyCode.F1; i < (uint)KeyCode.F12; i++) - { - if (knm == (KeyCode)i) - { - mK |= (KeyCode)i; - } - } - - knm &= ~mK; - uint.TryParse (knm.ToString (), out uint c); - string s = mK == KeyCode.Null ? "" : mK.ToString (); - - if (s != "" && (knm != KeyCode.Null || c > 0)) - { - s += ","; - } - - s += c == 0 ? knm == KeyCode.Null ? "" : knm.ToString () : ((char)c).ToString (); - - return s; - } -} diff --git a/Terminal.Gui/View/View.Keyboard.cs b/Terminal.Gui/View/View.Keyboard.cs index 1ebc77df0..87c5a1ce6 100644 --- a/Terminal.Gui/View/View.Keyboard.cs +++ b/Terminal.Gui/View/View.Keyboard.cs @@ -297,7 +297,6 @@ public partial class View // Keyboard APIs } // After - // TODO: Is ProcessKeyDown really the right name? if (RaiseKeyDownNotHandled (key) || key.Handled) { return true; @@ -426,7 +425,7 @@ public partial class View // Keyboard APIs /// /// If the focused sub view does not handle the key press, this method raises / /// to allow the - /// view to pre-process the key press. If /. + /// view to pre-process the key press. /// /// See for an overview of Terminal.Gui keyboard APIs. /// diff --git a/Terminal.Gui/Views/FileDialog.cs b/Terminal.Gui/Views/FileDialog.cs index 874e76dec..2ded19418 100644 --- a/Terminal.Gui/Views/FileDialog.cs +++ b/Terminal.Gui/Views/FileDialog.cs @@ -670,11 +670,11 @@ public class FileDialog : Dialog FinishAccept (); } - private void AcceptIf (Key keyEvent, KeyCode isKey) + private void AcceptIf (Key key, KeyCode isKey) { - if (!keyEvent.Handled && keyEvent.KeyCode == isKey) + if (!key.Handled && key.KeyCode == isKey) { - keyEvent.Handled = true; + key.Handled = true; // User hit Enter in text box so probably wants the // contents of the text box as their selection not diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index 564b1cfed..925bc203c 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -817,7 +817,6 @@ public class ListView : View, IDesignable } } - // TODO: This should be cancelable /// Invokes the event if it is defined. /// if the event was fired. public bool OnOpenSelectedItem () diff --git a/Terminal.Gui/Views/TableView/TableView.cs b/Terminal.Gui/Views/TableView/TableView.cs index ee39726c9..a33903d0b 100644 --- a/Terminal.Gui/Views/TableView/TableView.cs +++ b/Terminal.Gui/Views/TableView/TableView.cs @@ -1563,7 +1563,7 @@ public class TableView : View /// private TableSelection CreateTableSelection (int x, int y) { return CreateTableSelection (x, y, x, y); } - private bool CycleToNextTableEntryBeginningWith (Key keyEvent) + private bool CycleToNextTableEntryBeginningWith (Key key) { int row = SelectedRow; @@ -1573,7 +1573,7 @@ public class TableView : View return false; } - int match = CollectionNavigator.GetNextMatchingItem (row, (char)keyEvent); + int match = CollectionNavigator.GetNextMatchingItem (row, (char)key); if (match != -1) { diff --git a/Terminal.Gui/Views/TileView.cs b/Terminal.Gui/Views/TileView.cs index 6ad42ce98..60370b2cb 100644 --- a/Terminal.Gui/Views/TileView.cs +++ b/Terminal.Gui/Views/TileView.cs @@ -286,11 +286,11 @@ public class TileView : View //// BUGBUG: Why is this not handled by a key binding??? /// - protected override bool OnKeyDownNotHandled (Key keyEvent) + protected override bool OnKeyDownNotHandled (Key key) { var focusMoved = false; - if (keyEvent.KeyCode == ToggleResizable) + if (key.KeyCode == ToggleResizable) { foreach (TileViewLineView l in _splitterLines) { diff --git a/Terminal.Gui/Views/TreeView/TreeView.cs b/Terminal.Gui/Views/TreeView/TreeView.cs index 9b1bfce44..af6f80eb1 100644 --- a/Terminal.Gui/Views/TreeView/TreeView.cs +++ b/Terminal.Gui/Views/TreeView/TreeView.cs @@ -1182,7 +1182,7 @@ public class TreeView : View, ITreeView where T : class } /// - protected override bool OnKeyDown (Key keyEvent) + protected override bool OnKeyDown (Key key) { if (!Enabled) { @@ -1190,7 +1190,7 @@ public class TreeView : View, ITreeView where T : class } // If not a keybinding, is the key a searchable key press? - if (CollectionNavigatorBase.IsCompatibleKey (keyEvent) && AllowLetterBasedNavigation) + if (CollectionNavigatorBase.IsCompatibleKey (key) && AllowLetterBasedNavigation) { // If there has been a call to InvalidateMap since the last time // we need a new one to reflect the new exposed tree state @@ -1198,7 +1198,7 @@ public class TreeView : View, ITreeView where T : class // Find the current selected object within the tree int current = map.IndexOf (b => b.Model == SelectedObject); - int? newIndex = KeystrokeNavigator?.GetNextMatchingItem (current, (char)keyEvent); + int? newIndex = KeystrokeNavigator?.GetNextMatchingItem (current, (char)key); if (newIndex is int && newIndex != -1) { diff --git a/UICatalog/Scenarios/Keys.cs b/UICatalog/Scenarios/Keys.cs index e758af366..f2796a344 100644 --- a/UICatalog/Scenarios/Keys.cs +++ b/UICatalog/Scenarios/Keys.cs @@ -76,18 +76,18 @@ public class Keys : Scenario win.Add (label); int maxKeyString = Key.CursorRight.WithAlt.WithCtrl.WithShift.ToString ().Length; - ObservableCollection keyEventList = new (); + ObservableCollection keyList = new (); - var appKeyEventListView = new ListView + var appKeyListView = new ListView { X = 0, Y = Pos.Bottom (label), Width = "KeyDown:".Length + maxKeyString, Height = Dim.Fill (), - Source = new ListWrapper (keyEventList) + Source = new ListWrapper (keyList) }; - appKeyEventListView.ColorScheme = Colors.ColorSchemes ["TopLevel"]; - win.Add (appKeyEventListView); + appKeyListView.ColorScheme = Colors.ColorSchemes ["TopLevel"]; + win.Add (appKeyListView); // View key events... edit.KeyDown += (s, a) => { keyDownList.Add (a.ToString ()); }; @@ -100,7 +100,7 @@ public class Keys : Scenario // KeyDown label = new Label { - X = Pos.Right (appKeyEventListView) + 1, + X = Pos.Right (appKeyListView) + 1, Y = Pos.Top (label), Text = "TextView Key Down:" }; @@ -142,10 +142,9 @@ public class Keys : Scenario void KeyDownPressUp (Key args, string updown) { - // BUGBUG: KeyEvent.ToString is badly broken var msg = $"Key{updown,-7}: {args}"; - keyEventList.Add (msg); - appKeyEventListView.MoveDown (); + keyList.Add (msg); + appKeyListView.MoveDown (); onKeyDownNotHandledListView.MoveDown (); } diff --git a/UICatalog/Scenarios/Snake.cs b/UICatalog/Scenarios/Snake.cs index 998969d8d..906bc931d 100644 --- a/UICatalog/Scenarios/Snake.cs +++ b/UICatalog/Scenarios/Snake.cs @@ -357,30 +357,30 @@ public class Snake : Scenario } // BUGBUG: Should (can) this use key bindings instead. - protected override bool OnKeyDown (Key keyEvent) + protected override bool OnKeyDown (Key key) { - if (keyEvent.KeyCode == KeyCode.CursorUp) + if (key.KeyCode == KeyCode.CursorUp) { State.PlannedDirection = Direction.Up; return true; } - if (keyEvent.KeyCode == KeyCode.CursorDown) + if (key.KeyCode == KeyCode.CursorDown) { State.PlannedDirection = Direction.Down; return true; } - if (keyEvent.KeyCode == KeyCode.CursorLeft) + if (key.KeyCode == KeyCode.CursorLeft) { State.PlannedDirection = Direction.Left; return true; } - if (keyEvent.KeyCode == KeyCode.CursorRight) + if (key.KeyCode == KeyCode.CursorRight) { State.PlannedDirection = Direction.Right;