blob: 403c2b6e937b0ebd054530566716316f73ade2a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
using System.Diagnostics.Contracts;
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*=============================================================================
**
** Class: ConsoleKeyInfo
**
**
** Purpose: This value type represents a single key press, with modifier keys
** like Alt, Control, and Shift.
**
**
=============================================================================*/
namespace System {
[Serializable]
public struct ConsoleKeyInfo {
private char _keyChar;
private ConsoleKey _key;
private ConsoleModifiers _mods;
public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool control) {
// Limit ConsoleKey values to 0 to 255, but don't check whether the
// key is a valid value in our ConsoleKey enum. There are a few
// values in that enum that we didn't define, and reserved keys
// that might start showing up on keyboards in a few years.
if (((int)key) < 0 || ((int)key) > 255)
throw new ArgumentOutOfRangeException("key", Environment.GetResourceString("ArgumentOutOfRange_ConsoleKey"));
Contract.EndContractBlock();
_keyChar = keyChar;
_key = key;
_mods = 0;
if (shift)
_mods |= ConsoleModifiers.Shift;
if (alt)
_mods |= ConsoleModifiers.Alt;
if (control)
_mods |= ConsoleModifiers.Control;
}
public char KeyChar {
get { return _keyChar; }
}
public ConsoleKey Key {
get { return _key; }
}
public ConsoleModifiers Modifiers {
get { return _mods; }
}
public override bool Equals(Object value)
{
if (value is ConsoleKeyInfo)
return Equals((ConsoleKeyInfo)value);
else
return false;
}
public bool Equals(ConsoleKeyInfo obj)
{
return obj._keyChar == _keyChar && obj._key == _key && obj._mods == _mods;
}
public static bool operator ==(ConsoleKeyInfo a, ConsoleKeyInfo b)
{
return a.Equals(b);
}
public static bool operator !=(ConsoleKeyInfo a, ConsoleKeyInfo b)
{
return !(a == b);
}
public override int GetHashCode()
{
return (int)_keyChar | (int) _mods;
}
}
}
|