Merge pull request #931 from PowerShell/andschwa/fix-keys

Fix comparison of keys in dispatch dictionary
This commit is contained in:
Andy Schwartzmeyer
2016-04-29 11:41:39 -07:00
2 changed files with 12 additions and 19 deletions
@@ -82,12 +82,21 @@ namespace Microsoft.PowerShell
{
public bool Equals(ConsoleKeyInfo x, ConsoleKeyInfo y)
{
return x.Key == y.Key && x.KeyChar == y.KeyChar && x.Modifiers == y.Modifiers;
// We *must not* compare the KeyChar field as its value is platform-dependent.
// We compare exactly the ConsoleKey enum field (which is platform-agnostic)
// and the modifiers.
return x.Key == y.Key && x.Modifiers == y.Modifiers;
}
public int GetHashCode(ConsoleKeyInfo obj)
{
return obj.GetHashCode();
// Because a comparison of two ConsoleKeyInfo objects is a comparison of the
// combination of the ConsoleKey and Modifiers, we must combine their hashes.
// This is based on Tuple.GetHashCode
int h1 = obj.Key.GetHashCode();
int h2 = obj.Modifiers.GetHashCode();
return unchecked(((h1 << 5) + h1) ^ h2);
}
}
+1 -17
View File
@@ -11,22 +11,6 @@ namespace Microsoft.PowerShell
{
internal static class Keys
{
static Keys()
{
#if CORECLR
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Enter = new ConsoleKeyInfo((char)10, ConsoleKey.Enter, false, false, false);
}
else
{
Enter = new ConsoleKeyInfo((char)13, ConsoleKey.Enter, false, false, false);
}
#else
Enter = new ConsoleKeyInfo((char)13, ConsoleKey.Enter, false, false, false);
#endif
}
public static ConsoleKeyInfo A = new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false);
public static ConsoleKeyInfo B = new ConsoleKeyInfo('b', ConsoleKey.B, false, false, false);
public static ConsoleKeyInfo C = new ConsoleKeyInfo('c', ConsoleKey.C, false, false, false);
@@ -224,7 +208,7 @@ namespace Microsoft.PowerShell
public static ConsoleKeyInfo End = new ConsoleKeyInfo((char)0, ConsoleKey.End, false, false, false);
public static ConsoleKeyInfo CtrlEnd = new ConsoleKeyInfo((char)0, ConsoleKey.End, false, false, true);
public static ConsoleKeyInfo ShiftEnd = new ConsoleKeyInfo((char)0, ConsoleKey.End, true, false, false);
public static ConsoleKeyInfo Enter;
public static ConsoleKeyInfo Enter = new ConsoleKeyInfo((char)13, ConsoleKey.Enter, false, false, false);
public static ConsoleKeyInfo Escape = new ConsoleKeyInfo((char)27, ConsoleKey.Escape, false, false, false);
public static ConsoleKeyInfo Home = new ConsoleKeyInfo((char)0, ConsoleKey.Home, false, false, false);
public static ConsoleKeyInfo CtrlHome = new ConsoleKeyInfo((char)0, ConsoleKey.Home, false, false, true);