Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleans up key handling in drivers (was "fixes VkeyPacketSimulator is broken") #3078

Merged
merged 38 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
16ab9ea
Fixes #3054. VkeyPacketSimulator scenario is broken.
BDisp Dec 24, 2023
3f748be
Fix some key handle and unit tests.
BDisp Dec 24, 2023
6014ef4
Remove unnecessary conditional.
BDisp Dec 24, 2023
a938295
Improves key handling.
BDisp Dec 24, 2023
9349d54
Also allow map capslock to shift with accented characters.
BDisp Dec 25, 2023
f664a7b
Merge branch 'v2_develop' into v2_vkeypacketsimulator-fix_3054
BDisp Dec 26, 2023
bb00773
Change to MemberData.
BDisp Dec 27, 2023
723340a
Remove unnecessary using.
BDisp Dec 27, 2023
43afa07
Resolving merge conflicts.
BDisp Dec 28, 2023
1251370
Fix merge errors.
BDisp Dec 28, 2023
9216c9f
Fixes #3095. WindowsDriver should return the mask keys to IsShift, Is…
BDisp Dec 28, 2023
d256e25
Modifiers keys are valid to be handled on key down and key up.
BDisp Dec 28, 2023
c7bc119
Map KeyCode.Enter to ConsoleKey.Enter and vice versa.
BDisp Dec 28, 2023
afcdb7a
Merge branch 'v2_vkeypacketsimulator-fix_3054' of github.com:BDisp/Te…
tig Dec 31, 2023
cf684ed
Updated ScanCodeMapping table with readable constants
tig Dec 31, 2023
4db8ce3
Documented bugs
tig Dec 31, 2023
f5c92b9
Implemented mapping using MapVirtualKeyEx
tig Jan 1, 2024
5f0c035
Implemented mapping using MapVirtualKeyEx
tig Jan 1, 2024
9cd85fc
Changed KeyCode special keys to match ConsoleKey values + max unicode…
tig Jan 1, 2024
8893ffe
Fixed bogus CollectionNavigator impl and tests
tig Jan 1, 2024
466914a
Nuked DeleteChar. renamed InsertChar to Insert
tig Jan 1, 2024
1708762
KeyCode.Enter = ConsoleKey.Enter, not \n
tig Jan 2, 2024
da71023
Code cleanup
tig Jan 2, 2024
f817bc7
Added diag for keyboard layout name
tig Jan 2, 2024
5e94f72
Fixed AltGr support (hopefully)
tig Jan 2, 2024
64ec76d
Simplified code
tig Jan 3, 2024
9d3de85
Simplified KeyCode by removing ShiftKeys
tig Jan 3, 2024
a5424a5
Fixed TextView
tig Jan 3, 2024
ce8db7e
Code cleanup
tig Jan 3, 2024
85c8d3d
Fixes cursesdriver (somewhat)
tig Jan 3, 2024
198e261
Code cleanup
tig Jan 3, 2024
818e8fe
netdriver wip
tig Jan 4, 2024
b7fca27
Merge pull request #172 from tig/BDisp-v2_vkeypacketsimulator-fix_3054
BDisp Jan 4, 2024
8659916
Merged v2_develop
tig Jan 4, 2024
0d346ab
Fixed netdriver under WSL
tig Jan 4, 2024
31abd45
Turned off debug spew
tig Jan 4, 2024
d1f1ef4
Removed old code
tig Jan 4, 2024
223e8c6
Merge pull request #173 from tig/bDisp-v2_vkeypacketsimulator-fix_3054
BDisp Jan 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading