-
Notifications
You must be signed in to change notification settings - Fork 54
/
KeyboardShortcut.cs
179 lines (159 loc) · 7.19 KB
/
KeyboardShortcut.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// This is copied from BepInEx 5 and modified to work with BepInEx 6 nightlies where this type is missing
// This is a temporary fix until this struct is added to BepInEx 6 proper
using BepInEx.Logging;
using System.Reflection;
using UnityEngine;
using Logger = BepInEx.Logging.Logger;
namespace BepInEx.Configuration
{
/// <summary>
/// A keyboard shortcut that can be used in Update method to check if user presses a key combo. The shortcut is only
/// triggered when the user presses the exact combination. For example, <c>F + LeftCtrl</c> will trigger only if user
/// presses and holds only LeftCtrl, and then presses F. If any other keys are pressed, the shortcut will not trigger.
///
/// Can be used as a value of a setting in <see cref="ConfigFile.Bind{T}(ConfigDefinition,T,ConfigDescription)"/>
/// to allow user to change this shortcut and have the changes saved.
///
/// How to use: Use <see cref="IsDown"/> in this class instead of <see cref="Input.GetKeyDown(KeyCode)"/> in the Update loop.
/// </summary>
public struct KeyboardShortcut
{
static KeyboardShortcut()
{
TomlTypeConverter.AddConverter(
typeof(KeyboardShortcut),
new TypeConverter
{
ConvertToString = (o, type) => ((KeyboardShortcut)o).Serialize(),
ConvertToObject = (s, type) => Deserialize(s)
});
}
/// <summary>
/// Shortcut that never triggers.
/// </summary>
public static readonly KeyboardShortcut Empty = new KeyboardShortcut();
/// <summary>
/// All KeyCode values that can be used in a keyboard shortcut.
/// </summary>
public static readonly IEnumerable<KeyCode> AllKeyCodes = Enum.GetValues(typeof(KeyCode)) as KeyCode[];
// Don't block hotkeys if mouse is being pressed, e.g. when shooting and trying to strafe
public static readonly KeyCode[] ModifierBlockKeyCodes = AllKeyCodes.Except(new KeyCode[] {
KeyCode.Mouse0, KeyCode.Mouse1, KeyCode.Mouse2, KeyCode.Mouse3,
KeyCode.Mouse4, KeyCode.Mouse5, KeyCode.Mouse6, KeyCode.None }).ToArray();
private readonly KeyCode[] _allKeys;
#if true
private static MethodInfo logMethod = typeof(Logger).GetMethod(
"Log", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null,
new Type[] { typeof(LogLevel), typeof(object) }, null);
#endif
/// <summary>
/// Create a new keyboard shortcut.
/// </summary>
/// <param name="mainKey">Main key to press</param>
/// <param name="modifiers">Keys that should be held down before main key is registered</param>
public KeyboardShortcut(KeyCode mainKey, params KeyCode[] modifiers) : this(new[] { mainKey }.Concat(modifiers).ToArray())
{
if (mainKey == KeyCode.None && modifiers.Any())
throw new ArgumentException($"Can't set {nameof(mainKey)} to KeyCode.None if there are any {nameof(modifiers)}");
}
private KeyboardShortcut(KeyCode[] keys)
{
_allKeys = SanitizeKeys(keys);
}
private static KeyCode[] SanitizeKeys(params KeyCode[] keys)
{
return keys != null && keys.Length != 0 && keys[0] != KeyCode.None
? new KeyCode[] { keys[0] }.Concat(keys.Skip(1).Distinct().Where(x => x != keys[0]).OrderBy(x => (int)x)).ToArray()
: new KeyCode[] { KeyCode.None };
}
/// <summary>
/// Main key of the key combination. It has to be pressed / let go last for the combination to be triggered.
/// If the combination is empty, <see cref="KeyCode.None"/> is returned.
/// </summary>
public KeyCode MainKey => _allKeys != null && _allKeys.Length > 0 ? _allKeys[0] : KeyCode.None;
/// <summary>
/// Modifiers of the key combination, if any.
/// </summary>
public IEnumerable<KeyCode> Modifiers => _allKeys?.Skip(1) ?? Enumerable.Empty<KeyCode>();
/// <summary>
/// Attempt to deserialize key combination from the string.
/// </summary>
public static KeyboardShortcut Deserialize(string str)
{
try
{
KeyCode[] parts = str.Split(new[] { ' ', '+', ',', ';', '|' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => (KeyCode)Enum.Parse(typeof(KeyCode), x)).ToArray();
return new KeyboardShortcut(parts);
}
catch (SystemException ex)
{
#if false
Logger.Log(LogLevel.Error, "Failed to read keybind from settings: " + ex.Message);
#else
logMethod?.Invoke(null, new object[] { LogLevel.Error, "Failed to read keybind from settings: " + ex.Message });
#endif
}
return Empty;
}
/// <summary>
/// Serialize the key combination into a user readable string.
/// </summary>
public string Serialize()
{
return _allKeys != null
? string.Join(" + ", _allKeys.Select(x => x.ToString()).ToArray())
: string.Empty;
}
/// <summary>
/// Check if the main key was just pressed (Input.GetKeyDown), and specified modifier keys are all pressed
/// </summary>
public bool IsDown()
{
KeyCode mainKey = MainKey;
return mainKey != KeyCode.None && Input.GetKeyDown(mainKey) && ModifierKeyTest();
}
/// <summary>
/// Check if the main key is currently held down (Input.GetKey), and specified modifier keys are all pressed
/// </summary>
public bool IsPressed()
{
KeyCode mainKey = MainKey;
return mainKey != KeyCode.None && Input.GetKey(mainKey) && ModifierKeyTest();
}
/// <summary>
/// Check if the main key was just lifted (Input.GetKeyUp), and specified modifier keys are all pressed.
/// </summary>
public bool IsUp()
{
KeyCode mainKey = MainKey;
return mainKey != KeyCode.None && Input.GetKeyUp(mainKey) && ModifierKeyTest();
}
private bool ModifierKeyTest()
{
KeyCode mainKey = MainKey;
return
_allKeys.All(key => key == mainKey || Input.GetKey(key)) &&
ModifierBlockKeyCodes.Except(_allKeys).All(key => !Input.GetKey(key));
}
/// <inheritdoc />
public override string ToString()
{
return MainKey != KeyCode.None
? string.Join(" + ", _allKeys.Select(key => key.ToString()).ToArray())
: "Not set";
}
/// <inheritdoc />
public override bool Equals(object obj)
{
return obj is KeyboardShortcut shortcut && MainKey == shortcut.MainKey && Modifiers.SequenceEqual(shortcut.Modifiers);
}
/// <inheritdoc />
public override int GetHashCode()
{
return MainKey != KeyCode.None
? _allKeys.Aggregate(_allKeys.Length, (current, item) => unchecked(current * 31 + (int)item))
: 0;
}
}
}