Skip to content

Commit

Permalink
Fixes gui-cs#3054. VkeyPacketSimulator scenario is broken.
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp committed Dec 24, 2023
1 parent 7822cd8 commit 16ab9ea
Show file tree
Hide file tree
Showing 9 changed files with 830 additions and 327 deletions.
530 changes: 377 additions & 153 deletions Terminal.Gui/ConsoleDrivers/ConsoleKeyMapping.cs

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ static Attribute MakeColor (short foreground, short background)
background: CursesColorNumberToColorName (background));
}

/// <inheritdoc/>
/// <remarks>
/// In the CursesDriver, colors are encoded as an int.
/// The foreground color is stored in the most significant 4 bits,
Expand Down Expand Up @@ -545,9 +546,10 @@ internal void ProcessInput ()
}
} else if (wch >= (uint)KeyCode.A && wch <= (uint)KeyCode.Z) {
k = (KeyCode)wch | KeyCode.ShiftMask;
} else if (wch <= 'z') {
k = (KeyCode)wch & ~KeyCode.Space;
}
//else if (wch <= 'z') {
// k = (KeyCode)wch & ~KeyCode.Space;
//}
OnKeyDown (new Key (k));
OnKeyUp (new Key (k));
}
Expand All @@ -572,8 +574,7 @@ void HandleEscSeqResponse (ref int code, ref KeyCode k, ref int wch2, ref Key ke
false, false, false), cki);
}
} else {
k = ConsoleKeyMapping.MapConsoleKeyToKey (consoleKeyInfo.Key, out _);
k = ConsoleKeyMapping.MapKeyModifiers (consoleKeyInfo, k);
k = ConsoleKeyMapping.MapConsoleKeyInfoToKeyCode (consoleKeyInfo);
keyEventArgs = new (k);
OnKeyDown (keyEventArgs);
}
Expand Down Expand Up @@ -724,11 +725,9 @@ public override void SendKeys (char keyChar, ConsoleKey consoleKey, bool shift,
if (control) {
mod |= ConsoleModifiers.Control;
}
var cKeyInfo = ConsoleKeyMapping.GetConsoleKeyFromKey (keyChar, mod, out _);
key = ConsoleKeyMapping.MapConsoleKeyToKey ((ConsoleKey)cKeyInfo.Key, out bool mappable);
if (mappable) {
key = (KeyCode)cKeyInfo.KeyChar;
}
var cKeyInfo = new ConsoleKeyInfo (keyChar, consoleKey, shift, alt, control);
cKeyInfo = ConsoleKeyMapping.DecodeVKPacketToKConsoleKeyInfo (cKeyInfo);
key = ConsoleKeyMapping.MapConsoleKeyInfoToKeyCode (cKeyInfo);
} else {
key = (KeyCode)keyChar;
}
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/ConsoleDrivers/FakeDriver/FakeConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ public static void PushMockKeyPress (KeyCode key)
{
MockKeyPresses.Push (new ConsoleKeyInfo (
(char)(key & ~KeyCode.CtrlMask & ~KeyCode.ShiftMask & ~KeyCode.AltMask),
ConsoleKeyMapping.GetConsoleKeyFromKey (key).Key,
ConsoleKeyMapping.GetConsoleKeyInfoFromKeyCode (key).Key,
key.HasFlag (KeyCode.ShiftMask),
key.HasFlag (KeyCode.AltMask),
key.HasFlag (KeyCode.CtrlMask)));
Expand Down
44 changes: 22 additions & 22 deletions Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,39 +205,39 @@ KeyCode MapKey (ConsoleKeyInfo keyInfo)
{
switch (keyInfo.Key) {
case ConsoleKey.Escape:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Esc);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Esc);
case ConsoleKey.Tab:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Tab);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Tab);
case ConsoleKey.Clear:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Clear);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Clear);
case ConsoleKey.Home:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Home);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Home);
case ConsoleKey.End:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.End);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.End);
case ConsoleKey.LeftArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorLeft);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorLeft);
case ConsoleKey.RightArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorRight);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorRight);
case ConsoleKey.UpArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorUp);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorUp);
case ConsoleKey.DownArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorDown);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorDown);
case ConsoleKey.PageUp:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.PageUp);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PageUp);
case ConsoleKey.PageDown:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.PageDown);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PageDown);
case ConsoleKey.Enter:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Enter);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Enter);
case ConsoleKey.Spacebar:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, keyInfo.KeyChar == 0 ? KeyCode.Space : (KeyCode)keyInfo.KeyChar);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, keyInfo.KeyChar == 0 ? KeyCode.Space : (KeyCode)keyInfo.KeyChar);
case ConsoleKey.Backspace:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Backspace);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Backspace);
case ConsoleKey.Delete:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.DeleteChar);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.DeleteChar);
case ConsoleKey.Insert:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.InsertChar);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.InsertChar);
case ConsoleKey.PrintScreen:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.PrintScreen);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PrintScreen);

case ConsoleKey.Oem1:
case ConsoleKey.Oem2:
Expand All @@ -256,33 +256,33 @@ KeyCode MapKey (ConsoleKeyInfo keyInfo)
return KeyCode.Null;
}

return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)keyInfo.KeyChar));
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.KeyChar));
}

var key = keyInfo.Key;
if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
var delta = key - ConsoleKey.A;
if (keyInfo.KeyChar != (uint)key) {
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)keyInfo.KeyChar);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
}
if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control)
|| keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt)
|| keyInfo.Modifiers.HasFlag (ConsoleModifiers.Shift)) {
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)KeyCode.A + delta));
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)KeyCode.A + delta));
}
var alphaBase = ((keyInfo.Modifiers != ConsoleModifiers.Shift)) ? 'A' : 'a';
return (KeyCode)((uint)alphaBase + delta);
}

return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)keyInfo.KeyChar));
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.KeyChar));
}

private CursorVisibility _savedCursorVisibility;

void MockKeyPressedHandler (ConsoleKeyInfo consoleKeyInfo)
{
if (consoleKeyInfo.Key == ConsoleKey.Packet) {
consoleKeyInfo = ConsoleKeyMapping.FromVKPacketToKConsoleKeyInfo (consoleKeyInfo);
consoleKeyInfo = ConsoleKeyMapping.DecodeVKPacketToKConsoleKeyInfo (consoleKeyInfo);
}

var map = MapKey (consoleKeyInfo);
Expand Down
44 changes: 23 additions & 21 deletions Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
var alt = (mod & ConsoleModifiers.Alt) != 0;
var control = (mod & ConsoleModifiers.Control) != 0;

var cKeyInfo = ConsoleKeyMapping.GetConsoleKeyFromKey (consoleKeyInfo.KeyChar, consoleKeyInfo.Modifiers, out _);
var cKeyInfo = ConsoleKeyMapping.DecodeVKPacketToKConsoleKeyInfo (consoleKeyInfo);

return new ConsoleKeyInfo (cKeyInfo.KeyChar, cKeyInfo.Key, shift, alt, control);
}
Expand All @@ -1007,35 +1007,35 @@ KeyCode MapKey (ConsoleKeyInfo keyInfo)
{
switch (keyInfo.Key) {
case ConsoleKey.Escape:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Esc);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Esc);
case ConsoleKey.Tab:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Tab);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Tab);
case ConsoleKey.Home:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Home);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Home);
case ConsoleKey.End:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.End);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.End);
case ConsoleKey.LeftArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorLeft);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorLeft);
case ConsoleKey.RightArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorRight);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorRight);
case ConsoleKey.UpArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorUp);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorUp);
case ConsoleKey.DownArrow:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.CursorDown);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.CursorDown);
case ConsoleKey.PageUp:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.PageUp);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PageUp);
case ConsoleKey.PageDown:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.PageDown);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.PageDown);
case ConsoleKey.Enter:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Enter);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Enter);
case ConsoleKey.Spacebar:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, keyInfo.KeyChar == 0 ? KeyCode.Space : (KeyCode)keyInfo.KeyChar);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, keyInfo.KeyChar == 0 ? KeyCode.Space : (KeyCode)keyInfo.KeyChar);
case ConsoleKey.Backspace:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.Backspace);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.Backspace);
case ConsoleKey.Delete:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.DeleteChar);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.DeleteChar);
case ConsoleKey.Insert:
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, KeyCode.InsertChar);
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode.InsertChar);

case ConsoleKey.Oem1:
case ConsoleKey.Oem2:
Expand All @@ -1046,7 +1046,7 @@ KeyCode MapKey (ConsoleKeyInfo keyInfo)
case ConsoleKey.Oem7:
case ConsoleKey.Oem8:
case ConsoleKey.Oem102:
var ret = ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)keyInfo.KeyChar));
var ret = ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.KeyChar));
if (ret.HasFlag (KeyCode.ShiftMask)) {
ret &= ~KeyCode.ShiftMask;
}
Expand All @@ -1069,11 +1069,13 @@ KeyCode MapKey (ConsoleKeyInfo keyInfo)
return (KeyCode)(((uint)KeyCode.AltMask) | ((uint)KeyCode.A + delta));
}
if (keyInfo.Modifiers == (ConsoleModifiers.Shift | ConsoleModifiers.Alt)) {
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)KeyCode.A + delta));
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)KeyCode.A + delta));
}
if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
if (keyInfo.KeyChar == 0 || (keyInfo.KeyChar != 0 && keyInfo.KeyChar >= 1 && keyInfo.KeyChar <= 26)) {
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)KeyCode.A + delta));
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)KeyCode.A + delta));
} else if (keyInfo.KeyChar != 0) {
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
}
}

Expand All @@ -1098,15 +1100,15 @@ KeyCode MapKey (ConsoleKeyInfo keyInfo)
}
if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
if (keyInfo.KeyChar == 0 || keyInfo.KeyChar == 30 || keyInfo.KeyChar == ((uint)KeyCode.D0 + delta)) {
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)KeyCode.D0 + delta));
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)KeyCode.D0 + delta));
}
}
return (KeyCode)((uint)keyInfo.KeyChar);
}
if (key is >= ConsoleKey.F1 and <= ConsoleKey.F12) {
var delta = key - ConsoleKey.F1;
if ((keyInfo.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
return ConsoleKeyMapping.MapKeyModifiers (keyInfo, (KeyCode)((uint)KeyCode.F1 + delta));
return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)KeyCode.F1 + delta));
}

return (KeyCode)((uint)KeyCode.F1 + delta);
Expand Down
Loading

0 comments on commit 16ab9ea

Please sign in to comment.