Skip to content

Commit

Permalink
Fixes gui-cs#3095. WindowsDriver should return the mask keys to IsShi…
Browse files Browse the repository at this point in the history
…ft, IsAlt and IsCtrl return the right value.
  • Loading branch information
BDisp committed Dec 28, 2023
1 parent 1251370 commit 9216c9f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,15 +1063,15 @@ KeyCode MapKey (WindowsConsole.ConsoleKeyInfoEx keyInfoEx)

// If the key is JUST a modifier, return it as that key
if (key == (ConsoleKey)16) { // Shift
return KeyCode.ShiftKey;
return KeyCode.ShiftMask;
}

if (key == (ConsoleKey)17) { // Ctrl
return KeyCode.CtrlKey;
return KeyCode.CtrlMask;
}

if (key == (ConsoleKey)18) { // Alt
return KeyCode.AltKey;
return KeyCode.AltMask;
}

return ConsoleKeyMapping.MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.KeyChar));
Expand Down
48 changes: 48 additions & 0 deletions UnitTests/Input/KeyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,54 @@ public void TryParse_ShouldReturnTrue_WhenValidKey (string keyString, KeyCode ex
[InlineData ("Ctrl-Ctrl")]
public void TryParse_ShouldReturnFalse_On_InvalidKey (string keyString) => Assert.False (Key.TryParse (keyString, out var _));

[Theory]
[InlineData (KeyCode.ShiftKey, KeyCode.ShiftMask, true)]
[InlineData (KeyCode.ShiftKey | KeyCode.A, KeyCode.ShiftMask | KeyCode.A, true)]
public void IsShift_With_Wrong_And_Right_ShiftMask_NoShift (KeyCode wrongKeyCode, KeyCode rightKeyCode, bool isShift)
{
Assert.NotEqual (((Key)wrongKeyCode).IsShift, isShift);
Assert.NotEqual ((Key)(wrongKeyCode & ~KeyCode.ShiftKey), ((Key)wrongKeyCode).NoShift);
Assert.Equal (((Key)rightKeyCode).IsShift, isShift);
Assert.Equal ((Key)(rightKeyCode & ~KeyCode.ShiftMask), ((Key)rightKeyCode).NoShift);
}

[Theory]
[InlineData (KeyCode.AltKey, KeyCode.AltMask, true)]
[InlineData (KeyCode.AltKey | KeyCode.A, KeyCode.AltMask | KeyCode.A, true)]
public void IsAlt_With_Wrong_And_Right_AltMask_NoAlt (KeyCode wrongKeyCode, KeyCode rightKeyCode, bool isAlt)
{
Assert.NotEqual (((Key)wrongKeyCode).IsAlt, isAlt);
Assert.NotEqual ((Key)(wrongKeyCode & ~KeyCode.AltKey), ((Key)wrongKeyCode).NoAlt);
Assert.Equal (((Key)rightKeyCode).IsAlt, isAlt);
Assert.Equal ((Key)(rightKeyCode & ~KeyCode.AltMask), ((Key)rightKeyCode).NoAlt);
}

[Theory]
[InlineData (KeyCode.CtrlKey, KeyCode.CtrlMask, true)]
[InlineData (KeyCode.CtrlKey | KeyCode.A, KeyCode.CtrlMask | KeyCode.A, true)]
public void IsCtrl_With_Wrong_And_Right_CtrlMask_NoCtrl (KeyCode wrongKeyCode, KeyCode rightKeyCode, bool isCtrl)
{
Assert.NotEqual (((Key)wrongKeyCode).IsCtrl, isCtrl);
Assert.NotEqual ((Key)(wrongKeyCode & ~KeyCode.CtrlKey), ((Key)wrongKeyCode).NoCtrl);
Assert.Equal (((Key)rightKeyCode).IsCtrl, isCtrl);
Assert.Equal ((Key)(rightKeyCode & ~KeyCode.CtrlMask), ((Key)rightKeyCode).NoCtrl);
}

[Theory]
[InlineData (KeyCode.ShiftMask, true, false, false)]
[InlineData (KeyCode.ShiftMask | KeyCode.AltMask, true, true, false)]
[InlineData (KeyCode.ShiftMask | KeyCode.AltMask | KeyCode.CtrlMask, true, true, true)]
[InlineData (KeyCode.ShiftMask | KeyCode.CtrlMask, true, false, true)]
[InlineData (KeyCode.AltMask, false, true, false)]
[InlineData (KeyCode.AltMask | KeyCode.CtrlMask, false, true, true)]
[InlineData (KeyCode.CtrlMask, false, false, true)]
public void IsShift_IsAlt_IsCtrl (KeyCode keyCode, bool isShift, bool isAlt, bool isCtrl)
{
Assert.Equal (((Key)keyCode).IsShift, isShift);
Assert.Equal (((Key)keyCode).IsAlt, isAlt);
Assert.Equal (((Key)keyCode).IsCtrl, isCtrl);
}

[Theory]
[InlineData (KeyCode.A, KeyCode.A)]
[InlineData (KeyCode.F1, KeyCode.F1)]
Expand Down

0 comments on commit 9216c9f

Please sign in to comment.