diff --git a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs index 784e84edb..caf479143 100644 --- a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs @@ -400,14 +400,11 @@ namespace Terminal.Gui { void ProcessInput (ConsoleKeyInfo consoleKey) { + keyModifiers = new KeyModifiers (); var map = MapKey (consoleKey); if (map == (Key)0xffffffff) return; - if (keyModifiers == null) { - keyModifiers = new KeyModifiers (); - } - if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Alt)) { keyModifiers.Alt = true; } @@ -420,7 +417,6 @@ namespace Terminal.Gui { keyHandler (new KeyEvent (map, keyModifiers)); keyUpHandler (new KeyEvent (map, keyModifiers)); - keyModifiers = new KeyModifiers (); } public override Attribute GetAttribute () diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver.cs b/Terminal.Gui/ConsoleDrivers/NetDriver.cs index 3a7c326c3..613825820 100644 --- a/Terminal.Gui/ConsoleDrivers/NetDriver.cs +++ b/Terminal.Gui/ConsoleDrivers/NetDriver.cs @@ -243,7 +243,14 @@ namespace Terminal.Gui { } break; case uint n when (n >= '\u0001' && n <= '\u001a'): - if (consoleKeyInfo.Key == 0) { + if (consoleKeyInfo.Key == 0 && consoleKeyInfo.KeyChar == '\r') { + key = ConsoleKey.Enter; + newConsoleKeyInfo = new ConsoleKeyInfo (consoleKeyInfo.KeyChar, + key, + (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0, + (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0, + (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0); + } else if (consoleKeyInfo.Key == 0) { key = (ConsoleKey)(char)(consoleKeyInfo.KeyChar + (uint)ConsoleKey.A - 1); newConsoleKeyInfo = new ConsoleKeyInfo ((char)key, key, @@ -253,7 +260,7 @@ namespace Terminal.Gui { } break; case 27: - //case 91: + //case 91: ConsoleKeyInfo [] cki = new ConsoleKeyInfo [] { consoleKeyInfo }; ConsoleModifiers mod = consoleKeyInfo.Modifiers; int delay = 0; @@ -1435,6 +1442,7 @@ namespace Terminal.Gui { continue; } dirtyLine [row] = false; + System.Text.StringBuilder output = new System.Text.StringBuilder (); for (int col = 0; col < cols; col++) { if (contents [row, col, 2] != 1) { continue; @@ -1445,13 +1453,24 @@ namespace Terminal.Gui { for (; col < cols && contents [row, col, 2] == 1; col++) { var color = contents [row, col, 1]; if (color != redrawColor) { + if (!AlwaysSetPosition) { + Console.Write (output); + output = new System.Text.StringBuilder (); + } SetColor (color); } if (AlwaysSetPosition && !SetCursorPosition (col, row)) { return; } - Console.Write ((char)contents [row, col, 0]); + if (AlwaysSetPosition) { + Console.Write ((char)contents [row, col, 0]); + } else { + output.Append ((char)contents [row, col, 0]); + } contents [row, col, 2] = 0; + if (!AlwaysSetPosition && col == cols - 1) { + Console.Write (output); + } } } } @@ -1648,6 +1667,7 @@ namespace Terminal.Gui { { switch (inputEvent.EventType) { case NetEvents.EventType.Key: + keyModifiers = new KeyModifiers (); var map = MapKey (inputEvent.ConsoleKeyInfo); if (map == (Key)0xffffffff) { return; @@ -1655,7 +1675,6 @@ namespace Terminal.Gui { keyDownHandler (new KeyEvent (map, keyModifiers)); keyHandler (new KeyEvent (map, keyModifiers)); keyUpHandler (new KeyEvent (map, keyModifiers)); - keyModifiers = new KeyModifiers (); break; case NetEvents.EventType.Mouse: mouseHandler (ToDriverMouse (inputEvent.MouseEvent)); @@ -1998,7 +2017,7 @@ namespace Terminal.Gui { void IMainLoopDriver.MainIteration () { - if (inputResult.Count > 0) { + while (inputResult.Count > 0) { ProcessInput?.Invoke (inputResult.Dequeue ().Value); } } diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 0a87f4182..0db043020 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1593,7 +1593,7 @@ namespace Terminal.Gui { /// public event Action KeyDown; - /// Contains the details about the key that produced the event. + /// public override bool OnKeyDown (KeyEvent keyEvent) { if (!Enabled) { @@ -1617,7 +1617,7 @@ namespace Terminal.Gui { /// public event Action KeyUp; - /// Contains the details about the key that produced the event. + /// public override bool OnKeyUp (KeyEvent keyEvent) { if (!Enabled) { diff --git a/UICatalog/Properties/launchSettings.json b/UICatalog/Properties/launchSettings.json index 68c82f53f..73a06de90 100644 --- a/UICatalog/Properties/launchSettings.json +++ b/UICatalog/Properties/launchSettings.json @@ -1,7 +1,11 @@ { "profiles": { "UICatalog": { - "commandName": "Project" + "commandName": "Project", + }, + "UICatalog : -usc": { + "commandName": "Project", + "commandLineArgs": "-usc" } } } \ No newline at end of file diff --git a/UnitTests/ConsoleDriverTests.cs b/UnitTests/ConsoleDriverTests.cs index cf03f98cb..346bf6258 100644 --- a/UnitTests/ConsoleDriverTests.cs +++ b/UnitTests/ConsoleDriverTests.cs @@ -420,5 +420,86 @@ namespace Terminal.Gui.ConsoleDrivers { var cs = new ColorScheme (); Assert.Equal ("", cs.caller); } + + [Fact] + [AutoInitShutdown] + public void KeyModifiers_Resetting_At_New_Keystrokes () + { + bool? okInitialFocused = null; + bool? cancelInitialFocused = null; + var okClicked = false; + var closing = false; + var cursorRight = false; + var cancelHasFocus = false; + var closed = false; + + var top = Application.Top; + + var ok = new Button ("Ok"); + ok.Clicked += () => { + if (!okClicked) { + okClicked = true; + Application.RequestStop (); + } + }; + + var cancel = new Button ("Cancel"); + + var d = new Dialog ("Quit", cancel, ok); + d.KeyPress += (e) => { + if (e.KeyEvent.Key == (Key.Q | Key.CtrlMask)) { + if (!okClicked && !closing) { + okInitialFocused = ok.HasFocus; + cancelInitialFocused = cancel.HasFocus; + closing = true; + var mKeys = new Stack (); + var cki = new ConsoleKeyInfo ('\0', ConsoleKey.Enter, false, false, false); + mKeys.Push (cki); + cki = new ConsoleKeyInfo ('\0', ConsoleKey.RightArrow, false, false, false); + mKeys.Push (cki); + FakeConsole.MockKeyPresses = mKeys; + } + e.Handled = true; + } else if (e.KeyEvent.Key == Key.CursorRight) { + if (!cursorRight) { + cursorRight = true; + } else if (ok.HasFocus) { + e.Handled = true; + } else { + cancelHasFocus = true; + } + } + }; + d.Loaded += () => { + var mKeys = new Stack (); + var cki = new ConsoleKeyInfo ('q', ConsoleKey.Q, false, false, true); + mKeys.Push (cki); + FakeConsole.MockKeyPresses = mKeys; + }; + d.Closed += (_) => { + if (okClicked && closing) { + closed = true; + } + }; + + top.Ready += () => Application.Run (d); + + Application.Iteration += () => { + if (closed) { + Application.RequestStop (); + } + }; + + Application.Run (); + + Assert.False (okInitialFocused); + Assert.True (cancelInitialFocused); + Assert.True (okClicked); + Assert.True (closing); + Assert.True (cursorRight); + Assert.True (cancelHasFocus); + Assert.True (closed); + Assert.Empty (FakeConsole.MockKeyPresses); + } } }