-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathKeyboardHotkey.cs
More file actions
48 lines (38 loc) · 972 Bytes
/
KeyboardHotkey.cs
File metadata and controls
48 lines (38 loc) · 972 Bytes
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
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Windows.Forms;
namespace ReClassNET.Input
{
public class KeyboardHotkey
{
private readonly HashSet<Keys> keys = new HashSet<Keys>();
public IEnumerable<Keys> Keys => keys;
public bool IsEmpty => keys.Count == 0;
public void Clear() => keys.Clear();
public bool AddKey(Keys key) => keys.Add(key);
public bool Matches(Keys[] pressedKeys)
{
Contract.Requires(pressedKeys != null);
if (keys.Count == 0 || keys.Count > pressedKeys.Length)
{
return false;
}
return keys.All(pressedKeys.Contains);
}
public KeyboardHotkey Clone()
{
var copy = new KeyboardHotkey();
foreach (var key in Keys) copy.AddKey(key);
return copy;
}
public override string ToString()
{
if (keys.Count == 0)
{
return string.Empty;
}
return keys.Select(k => k.ToString()).Aggregate((s1, s2) => $"{s1} + {s2}");
}
}
}