-
Notifications
You must be signed in to change notification settings - Fork 0
/
HotKey.cs
73 lines (57 loc) · 1.89 KB
/
HotKey.cs
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
using System.Text;
using System.Text.Json.Serialization;
using VirtualKeys.Extensions;
namespace VirtualKeys;
public class HotKey : IEquatable<HotKey>
{
public static HotKey Empty { get; } = new (Modifiers.None, Key.NOKEY);
public HotKey(Modifiers modifiers, Key key)
{
if (key.IsModifier())
{
modifiers |= key.ToModifiers();
key = Key.NOKEY;
}
Modifiers = modifiers;
Key = key;
}
public Modifiers Modifiers { get; }
public Key Key { get; }
[JsonIgnore]
public bool IsEmpty => Modifiers == Modifiers.None && Key == Key.NOKEY;
#region Equals
public bool Equals(HotKey? other)
{
if (other is null) return false;
if (ReferenceEquals(this, other)) return true;
return Modifiers.GetKeyFlags().SequenceEqual(other.Modifiers.GetKeyFlags())
&& Key == other.Key;
}
public override bool Equals(object? obj)
{
if (obj is null) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == this.GetType() && Equals((HotKey)obj);
}
public static bool operator ==(HotKey? left, HotKey? right) => left?.Equals(right) ?? false;
public static bool operator !=(HotKey? left, HotKey? right) => !(left == right);
public override int GetHashCode()
{
return HashCode.Combine((int)Modifiers, (int)Key);
}
#endregion
public override string ToString()
{
if (IsEmpty) return "(None)";
var sb = new StringBuilder();
if (Modifiers != Modifiers.None)
sb.Append(Modifiers.ToDisplayName());
if (Key != Key.NOKEY)
{
sb.Append(" + ");
sb.Append(VirtualKeyConverter.ToChar(Key));
}
sb.Append(Modifiers.HasFlag(Modifiers.NoRepeat) ? " (No repeatable)" : " (Repeatable)");
return sb.ToString();
}
}