NetDriver fixes keys modifiers not reseting and enter key on Windows Terminal. (#1484)

* Fixes keys modifiers not reseting and enter key on Windows Terminal.

* Fixes summary warnings.

* Ensures MainIteration processes  input while count greater than zero.

* Adding UseSystemConsole to the launch settings profile.

* Improves the performance with Console.Write by writing faster.
This commit is contained in:
BDisp
2021-10-25 20:31:49 +01:00
committed by GitHub
parent b5799c3ded
commit 2ef4edd08e
5 changed files with 113 additions and 13 deletions

View File

@@ -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 ()

View File

@@ -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);
}
}

View File

@@ -1593,7 +1593,7 @@ namespace Terminal.Gui {
/// </summary>
public event Action<KeyEventEventArgs> KeyDown;
/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
/// <inheritdoc/>
public override bool OnKeyDown (KeyEvent keyEvent)
{
if (!Enabled) {
@@ -1617,7 +1617,7 @@ namespace Terminal.Gui {
/// </summary>
public event Action<KeyEventEventArgs> KeyUp;
/// <param name="keyEvent">Contains the details about the key that produced the event.</param>
/// <inheritdoc/>
public override bool OnKeyUp (KeyEvent keyEvent)
{
if (!Enabled) {

View File

@@ -1,7 +1,11 @@
{
"profiles": {
"UICatalog": {
"commandName": "Project"
"commandName": "Project",
},
"UICatalog : -usc": {
"commandName": "Project",
"commandLineArgs": "-usc"
}
}
}

View File

@@ -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<ConsoleKeyInfo> ();
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<ConsoleKeyInfo> ();
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);
}
}
}