-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoiceModUserControl.KeyBind.cs
223 lines (191 loc) · 7.28 KB
/
VoiceModUserControl.KeyBind.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using NvkCommon;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using static NvkCommon.Log;
namespace VoiceMod
{
partial class VoiceModUserControl
{
private Dictionary<string, PredefinedWrapper> mapKeyBindToPredefinedWrapper = new Dictionary<string, PredefinedWrapper>();
private void InitializeKeyBind()
{
ParentForm.Deactivate += ParentForm_Deactivate;
}
private void ParentForm_Deactivate(object sender, EventArgs e)
{
Log.PrintLine(TAG, LogLevel.Information, $"ParentForm_Deactivate: e={e}");
HotKeyListenerStop();
}
private bool HasKeyBinds
{
get
{
return mapKeyBindToPredefinedWrapper.Count > 0;
}
}
private bool KeyBindGet(string keyBind, out PredefinedWrapper predefinedWrapper)
{
return mapKeyBindToPredefinedWrapper.TryGetValue(keyBind, out predefinedWrapper);
}
private bool KeyBindPut(string keyBind, PredefinedWrapper predefinedWrapper)
{
if (String.IsNullOrEmpty(keyBind) || predefinedWrapper == null) return false;
predefinedWrapper.Predefined.KeyBind = keyBind;
SavePredefineds();
var isNewEntry = !mapKeyBindToPredefinedWrapper.ContainsKey(keyBind);
mapKeyBindToPredefinedWrapper.Add(keyBind, predefinedWrapper);
if (isNewEntry)
{
if (mapKeyBindToPredefinedWrapper.Count == 1)
{
HotKeyListenerStart();
}
}
return isNewEntry;
}
private bool KeyBindRemove(string keyBind)
{
if (String.IsNullOrEmpty(keyBind)) return false;
PredefinedWrapper predefinedWrapper;
if (mapKeyBindToPredefinedWrapper.TryGetValue(keyBind, out predefinedWrapper))
{
predefinedWrapper.Predefined.KeyBind = String.Empty;
SavePredefineds();
mapKeyBindToPredefinedWrapper.Remove(keyBind);
if (mapKeyBindToPredefinedWrapper.Count == 0)
{
HotKeyListenerStop();
}
return true;
}
return false;
}
private bool IsKeyBindRecording
{
get
{
return textBoxPredefinedKeyBind.BackColor == Color.Red;
}
set
{
if (value)
{
if (textBoxPredefinedKeyBind.BackColor != Color.Red)
{
textBoxPredefinedKeyBind.BackColor = Color.Red;
buttonPredefinedKeyBindAdd.Text = "*";
textBoxPredefinedKeyBind.Text = null;
HotKeyListenerStart();
}
}
else
{
if (textBoxPredefinedKeyBind.BackColor != Color.Green)
{
textBoxPredefinedKeyBind.BackColor = Color.Green;
buttonPredefinedKeyBindAdd.Text = "+";
if (!HasKeyBinds)
{
HotKeyListenerStop();
}
}
}
}
}
private KeyboardHook keyboardHook = new KeyboardHook();
private void HotKeyListenerStart()
{
Log.PrintLine(TAG, LogLevel.Information, $"HotKeyListenerStart()");
if (keyboardHook.IsStarted) return;
keyboardHook.KeysChanged += KeyboardHook_KeyIntercepted;
keyboardHook.Start();
}
private void HotKeyListenerStop()
{
Log.PrintLine(TAG, LogLevel.Information, $"HotKeyListenerStop()");
keyboardHook.KeysChanged -= KeyboardHook_KeyIntercepted;
keyboardHook.Stop();
IsKeyBindRecording = false;
}
private void KeyboardHook_KeyIntercepted(KeyboardHook.KeysChangedEventArgs e)
{
//Log.PrintLine(TAG, LogLevel.Information, $"KeyboardHook_KeyIntercepted: e={e}");
//Log.PrintLine(TAG, LogLevel.Information, $"KeyboardHook_KeyIntercepted: e.KeysCurrentlyDown={e.KeysCurrentlyDown}");
//Log.PrintLine(TAG, LogLevel.Information, $"KeyboardHook_KeyIntercepted: e.CapsLockOn={e.CapsLockOn}");
var keyBind = String.Join("+", e.KeysCurrentlyDown);
Log.PrintLine(TAG, LogLevel.Information, $"KeyboardHook_KeyIntercepted: keyBind={Utils.Quote(keyBind)}");
if (IsKeyBindRecording)
{
if (e.MostRecentKeyDirection == KeyboardHook.KeyDirection.Down && !String.IsNullOrEmpty(keyBind))
{
textBoxPredefinedKeyBind.Text = keyBind;
}
}
else
{
if (String.IsNullOrEmpty(keyBind))
{
comboBoxPredefined.SelectedIndex = 0;
}
else
{
PredefinedWrapper predefinedWrapper;
if (KeyBindGet(keyBind, out predefinedWrapper))
{
Log.PrintLine(TAG, LogLevel.Information, $"KeyboardHook_KeyIntercepted: predefinedWrapper={predefinedWrapper}");
CurrentlySelectedPredefinedWrapper = predefinedWrapper;
}
}
}
}
private void buttonPredefinedKeyBindAdd_Click(object sender, EventArgs e)
{
if (IsKeyBindRecording)
{
var keyBind = textBoxPredefinedKeyBind.Text;
var predefinedWrapper = CurrentlySelectedPredefinedWrapper;
if (predefinedWrapper != null)
{
KeyBindPut(keyBind, predefinedWrapper);
}
IsKeyBindRecording = false;
}
else
{
IsKeyBindRecording = true;
}
}
private void buttonPredefinedKeyBindRemove_Click(object sender, EventArgs e)
{
if (IsKeyBindRecording)
{
IsKeyBindRecording = false;
var predefinedWrapper = CurrentlySelectedPredefinedWrapper;
if (predefinedWrapper != null)
{
textBoxPredefinedKeyBind.Text = predefinedWrapper?.Predefined.KeyBind;
}
}
else
{
var keyBindToRemove = textBoxPredefinedKeyBind.Text;
textBoxPredefinedKeyBind.Text = null;
var predefinedWrapper = CurrentlySelectedPredefinedWrapper;
if (predefinedWrapper != null)
{
KeyBindRemove(keyBindToRemove);
}
}
}
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
}