WingMan – Diff between revs 9 and 10
?pathlinks?
Rev 9 | Rev 10 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
3 | using System.Drawing; |
3 | using System.Drawing; |
|
4 | using System.IO; |
4 | using System.IO; |
|
5 | using System.Linq; |
5 | using System.Linq; |
|
6 | using System.Net; |
6 | using System.Net; |
|
7 | using System.Threading; |
7 | using System.Threading; |
|
8 | using System.Threading.Tasks; |
8 | using System.Threading.Tasks; |
|
9 | using System.Windows.Forms; |
9 | using System.Windows.Forms; |
|
10 | using Gma.System.MouseKeyHook; |
10 | using Gma.System.MouseKeyHook; |
|
11 | using MQTTnet.Extensions.ManagedClient; |
11 | using MQTTnet.Extensions.ManagedClient; |
|
12 | using MQTTnet.Server; |
12 | using MQTTnet.Server; |
|
13 | using WingMan.Communication; |
13 | using WingMan.Communication; |
|
14 | using WingMan.Lobby; |
14 | using WingMan.Lobby; |
|
15 | using WingMan.MouseKey; |
15 | using WingMan.MouseKey; |
|
16 | using WingMan.Properties; |
16 | using WingMan.Properties; |
|
17 | using WingMan.Utilities; |
17 | using WingMan.Utilities; |
|
18 | |
18 | |
|
19 | namespace WingMan |
19 | namespace WingMan |
|
20 | { |
20 | { |
|
21 | public partial class WingManForm : Form |
21 | public partial class WingManForm : Form |
|
22 | { |
22 | { |
|
23 | public WingManForm() |
23 | public WingManForm() |
|
24 | { |
24 | { |
|
25 | InitializeComponent(); |
25 | InitializeComponent(); |
|
26 | |
26 | |
|
27 | FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
27 | FormTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); |
|
28 | FormCancellationTokenSource = new CancellationTokenSource(); |
28 | FormCancellationTokenSource = new CancellationTokenSource(); |
|
29 | |
29 | |
|
30 | MqttCommunication = new MqttCommunication(FormTaskScheduler, FormCancellationTokenSource.Token); |
30 | MqttCommunication = new MqttCommunication(FormTaskScheduler, FormCancellationTokenSource.Token); |
|
31 | MqttCommunication.OnClientAuthenticationFailed += OnMqttClientAuthenticationFailed; |
31 | MqttCommunication.OnClientAuthenticationFailed += OnMqttClientAuthenticationFailed; |
|
32 | MqttCommunication.OnClientConnectionFailed += OnMqttClientConnectionFailed; |
32 | MqttCommunication.OnClientConnectionFailed += OnMqttClientConnectionFailed; |
|
33 | MqttCommunication.OnClientDisconnected += OnMqttClientDisconnected; |
33 | MqttCommunication.OnClientDisconnected += OnMqttClientDisconnected; |
|
34 | MqttCommunication.OnClientConnected += OnMqttClientConnected; |
34 | MqttCommunication.OnClientConnected += OnMqttClientConnected; |
|
35 | MqttCommunication.OnServerAuthenticationFailed += OnMqttServerAuthenticationFailed; |
35 | MqttCommunication.OnServerAuthenticationFailed += OnMqttServerAuthenticationFailed; |
|
36 | MqttCommunication.OnServerStopped += OnMqttServerStopped; |
36 | MqttCommunication.OnServerStopped += OnMqttServerStopped; |
|
37 | MqttCommunication.OnServerStarted += OnMqttServerStarted; |
37 | MqttCommunication.OnServerStarted += OnMqttServerStarted; |
|
38 | MqttCommunication.OnServerClientConnected += OnMqttServerClientConnected; |
38 | MqttCommunication.OnServerClientConnected += OnMqttServerClientConnected; |
|
39 | MqttCommunication.OnServerClientDisconnected += OnMqttServerClientDisconnected; |
39 | MqttCommunication.OnServerClientDisconnected += OnMqttServerClientDisconnected; |
|
40 | |
40 | |
|
41 | LocalMouseKeyBindings = new MouseKeyBindings(new List<MouseKeyBinding>()); |
41 | LocalKeyBindings = new LocalKeyBindings(new List<KeyBinding>()); |
|
42 | RemoteMouseKeyBindings = new RemoteMouseKeyBindings(new List<RemoteMouseKeyBinding>()); |
42 | RemoteKeyBindings = new RemoteKeyBindings(new List<RemoteKeyBinding>()); |
|
43 | |
43 | |
|
44 | LocalListBoxBindingSource = new BindingSource |
44 | LocalListBoxBindingSource = new BindingSource |
|
45 | { |
45 | { |
|
46 | DataSource = LocalMouseKeyBindings.Bindings |
46 | DataSource = LocalKeyBindings.Bindings |
|
47 | }; |
47 | }; |
|
48 | LocalBindingsListBox.DisplayMember = "DisplayName"; |
48 | LocalBindingsListBox.DisplayMember = "DisplayName"; |
|
49 | LocalBindingsListBox.ValueMember = "Keys"; |
49 | LocalBindingsListBox.ValueMember = "Keys"; |
|
50 | LocalBindingsListBox.DataSource = LocalListBoxBindingSource; |
50 | LocalBindingsListBox.DataSource = LocalListBoxBindingSource; |
|
51 | |
51 | |
|
52 | MouseKeyBindingsExchange = new MouseKeyBindingsExchange |
52 | KeyBindingsExchange = new KeyBindingsExchange |
|
53 | { |
53 | { |
|
54 | ExchangeBindings = new List<MouseKeyBindingExchange>() |
54 | ExchangeBindings = new List<KeyBindingExchange>() |
|
55 | }; |
55 | }; |
|
56 | |
56 | |
|
57 | RemoteBindingsComboBoxSource = new BindingSource |
57 | RemoteBindingsComboBoxSource = new BindingSource |
|
58 | { |
58 | { |
|
59 | DataSource = MouseKeyBindingsExchange.ExchangeBindings |
59 | DataSource = KeyBindingsExchange.ExchangeBindings |
|
60 | }; |
60 | }; |
|
61 | RemoteBindingsComboBox.DisplayMember = "Nick"; |
61 | RemoteBindingsComboBox.DisplayMember = "Nick"; |
|
62 | RemoteBindingsComboBox.ValueMember = "MouseKeyBindings"; |
62 | RemoteBindingsComboBox.ValueMember = "KeyBindings"; |
|
63 | RemoteBindingsComboBox.DataSource = RemoteBindingsComboBoxSource; |
63 | RemoteBindingsComboBox.DataSource = RemoteBindingsComboBoxSource; |
|
64 | |
64 | |
|
65 | // Start lobby message synchronizer. |
65 | // Start lobby message synchronizer. |
|
66 | LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MqttCommunication, FormTaskScheduler, |
66 | LobbyMessageSynchronizer = new LobbyMessageSynchronizer(MqttCommunication, FormTaskScheduler, |
|
67 | FormCancellationTokenSource.Token); |
67 | FormCancellationTokenSource.Token); |
|
68 | LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived; |
68 | LobbyMessageSynchronizer.OnLobbyMessageReceived += OnLobbyMessageReceived; |
|
69 | |
69 | |
|
70 | // Start mouse key bindings synchronizer. |
70 | // Start mouse key bindings synchronizer. |
|
71 | MouseKeyBindingsSynchronizer = new MouseKeyBindingsSynchronizer(LocalMouseKeyBindings, MqttCommunication, |
71 | KeyBindingsSynchronizer = new KeyBindingsSynchronizer(LocalKeyBindings, MqttCommunication, |
|
72 | FormTaskScheduler, FormCancellationTokenSource.Token); |
72 | FormTaskScheduler, FormCancellationTokenSource.Token); |
|
73 | MouseKeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized; |
73 | KeyBindingsSynchronizer.OnMouseKeyBindingsSynchronized += OnMouseKeyBindingsSynchronized; |
|
- | 74 | |
||
- | 75 | // Start mouse key interceptor. |
||
- | 76 | KeyInterceptor = new KeyInterceptor(RemoteKeyBindings, MqttCommunication, FormTaskScheduler, |
||
- | 77 | FormCancellationTokenSource.Token); |
||
- | 78 | KeyInterceptor.OnMouseKeyBindingMatched += OnMouseKeyBindingMatched; |
||
74 | |
79 | |
|
- | 80 | // Start mouse key simulator. |
||
- | 81 | KeySimulator = new KeySimulator(LocalKeyBindings, MqttCommunication, FormTaskScheduler, |
||
- | 82 | FormCancellationTokenSource.Token); |
||
75 | // Start key binding simulator. |
83 | KeySimulator.OnMouseKeyBindingExecuting += OnMouseKeyBindingExecuting; |
|
76 | } |
84 | } |
|
77 | |
85 | |
|
78 | private static CancellationTokenSource FormCancellationTokenSource { get; set; } |
86 | private static CancellationTokenSource FormCancellationTokenSource { get; set; } |
|
79 | |
87 | |
|
80 | private static TaskScheduler FormTaskScheduler { get; set; } |
88 | private static TaskScheduler FormTaskScheduler { get; set; } |
|
81 | |
89 | |
|
82 | private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } |
90 | private static IKeyboardMouseEvents MouseKeyApplicationHook { get; set; } |
|
83 | |
91 | |
|
84 | private List<string> MouseKeyCombo { get; set; } |
92 | private List<string> MouseKeyCombo { get; set; } |
|
85 | |
93 | |
|
86 | private MouseKeyBindings LocalMouseKeyBindings { get; } |
94 | private LocalKeyBindings LocalKeyBindings { get; } |
|
87 | |
95 | |
|
88 | private RemoteMouseKeyBindings RemoteMouseKeyBindings { get; } |
96 | private RemoteKeyBindings RemoteKeyBindings { get; } |
|
89 | |
97 | |
|
90 | private BindingSource LocalListBoxBindingSource { get; } |
98 | private BindingSource LocalListBoxBindingSource { get; } |
|
91 | |
99 | |
|
92 | private BindingSource RemoteBindingsComboBoxSource { get; } |
100 | private BindingSource RemoteBindingsComboBoxSource { get; } |
|
93 | |
101 | |
|
94 | private MouseKeyBindingsExchange MouseKeyBindingsExchange { get; } |
102 | private KeyBindingsExchange KeyBindingsExchange { get; } |
|
95 | |
103 | |
|
96 | public MqttCommunication MqttCommunication { get; set; } |
104 | public MqttCommunication MqttCommunication { get; set; } |
|
97 | |
105 | |
|
98 | public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; } |
106 | public LobbyMessageSynchronizer LobbyMessageSynchronizer { get; set; } |
|
99 | |
107 | |
|
100 | public MouseKeyBindingsSynchronizer MouseKeyBindingsSynchronizer { get; set; } |
- | ||
101 | |
- | ||
102 | private async Task SaveLocalMouseKeyBindings() |
- | ||
103 | { |
- | ||
104 | try |
108 | public KeyBindingsSynchronizer KeyBindingsSynchronizer { get; set; } |
|
105 | { |
- | ||
106 | using (var memoryStream = new MemoryStream()) |
- | ||
- | 109 | |
||
- | 110 | public KeyInterceptor KeyInterceptor { get; set; } |
||
- | 111 | |
||
107 | { |
112 | public KeySimulator KeySimulator { get; set; } |
|
- | 113 | |
||
- | 114 | /// <summary> |
||
- | 115 | /// Clean up any resources being used. |
||
- | 116 | /// </summary> |
||
- | 117 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
- | 118 | protected override void Dispose(bool disposing) |
||
108 | MouseKeyBindings.XmlSerializer.Serialize(memoryStream, LocalMouseKeyBindings); |
119 | { |
|
109 | |
120 | FormCancellationTokenSource?.Dispose(); |
|
110 | memoryStream.Position = 0L; |
121 | FormCancellationTokenSource = null; |
|
111 | |
122 | |
|
112 | using (var fileStream = new FileStream("LocalMouseKeyBindings.xml", FileMode.Create)) |
123 | if (disposing && components != null) |
|
- | 124 | { |
||
- | 125 | components.Dispose(); |
||
113 | { |
126 | components = null; |
|
- | 127 | } |
||
114 | await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false); |
128 | |
|
115 | } |
129 | base.Dispose(disposing); |
|
116 | } |
130 | } |
|
117 | } |
131 | |
|
118 | catch (Exception) |
132 | private void OnMouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args) |
|
- | 133 | { |
||
- | 134 | ActivityTextBox.AppendText( |
||
- | 135 | $"{Strings.Executing_binding_from_remote_client} : {args.Nick} : {args.Name}{Environment.NewLine}"); |
||
- | 136 | } |
||
- | 137 | |
||
119 | { |
138 | private void OnMouseKeyBindingMatched(object sender, KeyBindingMatchedEventArgs args) |
|
120 | ActivityTextBox.AppendText( |
139 | { |
|
121 | $"{Strings.Failed_saving_local_bindings}{Environment.NewLine}"); |
140 | ActivityTextBox.AppendText( |
|
122 | } |
141 | $"{Strings.Matched_remote_key_binding} : {args.Nick} : {args.Name} : {string.Join(" + ", args.KeyCombo)}{Environment.NewLine}"); |
|
123 | } |
142 | } |
|
124 | |
143 | |
|
125 | private void OnMqttServerClientDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
144 | private void OnMqttServerClientDisconnected(object sender, MqttClientDisconnectedEventArgs e) |
|
126 | { |
145 | { |
|
127 | ActivityTextBox.AppendText( |
146 | ActivityTextBox.AppendText( |
|
128 | $"{Strings.Client_disconnected}{Environment.NewLine}"); |
147 | $"{Strings.Client_disconnected}{Environment.NewLine}"); |
|
129 | } |
148 | } |
|
130 | |
149 | |
|
131 | private void OnMqttServerClientConnected(object sender, MqttClientConnectedEventArgs e) |
150 | private void OnMqttServerClientConnected(object sender, MqttClientConnectedEventArgs e) |
|
132 | { |
151 | { |
|
133 | ActivityTextBox.AppendText( |
152 | ActivityTextBox.AppendText( |
|
134 | $"{Strings.Client_connected}{Environment.NewLine}"); |
153 | $"{Strings.Client_connected}{Environment.NewLine}"); |
|
135 | } |
154 | } |
|
136 | |
155 | |
|
137 | private void OnMqttServerStarted(object sender, EventArgs e) |
156 | private void OnMqttServerStarted(object sender, EventArgs e) |
|
138 | { |
157 | { |
|
139 | ActivityTextBox.AppendText( |
158 | ActivityTextBox.AppendText( |
|
140 | $"{Strings.Server_started}{Environment.NewLine}"); |
159 | $"{Strings.Server_started}{Environment.NewLine}"); |
|
141 | } |
160 | } |
|
142 | |
161 | |
|
143 | private void OnMqttServerStopped(object sender, EventArgs e) |
162 | private void OnMqttServerStopped(object sender, EventArgs e) |
|
144 | { |
163 | { |
|
145 | ActivityTextBox.AppendText( |
164 | ActivityTextBox.AppendText( |
|
146 | $"{Strings.Server_stopped}{Environment.NewLine}"); |
165 | $"{Strings.Server_stopped}{Environment.NewLine}"); |
|
147 | } |
166 | } |
|
148 | |
167 | |
|
149 | private void OnMqttClientConnected(object sender, MQTTnet.Client.MqttClientConnectedEventArgs e) |
168 | private void OnMqttClientConnected(object sender, MQTTnet.Client.MqttClientConnectedEventArgs e) |
|
150 | { |
169 | { |
|
151 | ActivityTextBox.AppendText( |
170 | ActivityTextBox.AppendText( |
|
152 | $"{Strings.Client_connected}{Environment.NewLine}"); |
171 | $"{Strings.Client_connected}{Environment.NewLine}"); |
|
153 | } |
172 | } |
|
154 | |
173 | |
|
155 | private void OnMqttClientDisconnected(object sender, MQTTnet.Client.MqttClientDisconnectedEventArgs e) |
174 | private void OnMqttClientDisconnected(object sender, MQTTnet.Client.MqttClientDisconnectedEventArgs e) |
|
156 | { |
175 | { |
|
157 | ActivityTextBox.AppendText( |
176 | ActivityTextBox.AppendText( |
|
158 | $"{Strings.Client_disconnected}{Environment.NewLine}"); |
177 | $"{Strings.Client_disconnected}{Environment.NewLine}"); |
|
159 | } |
178 | } |
|
160 | |
179 | |
|
161 | private void OnMqttClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e) |
180 | private void OnMqttClientConnectionFailed(object sender, MqttManagedProcessFailedEventArgs e) |
|
162 | { |
181 | { |
|
163 | ActivityTextBox.AppendText( |
182 | ActivityTextBox.AppendText( |
|
164 | $"{Strings.Client_connection_failed}{Environment.NewLine}"); |
183 | $"{Strings.Client_connection_failed}{Environment.NewLine}"); |
|
165 | } |
184 | } |
|
166 | |
185 | |
|
167 | private void OnMqttServerAuthenticationFailed(object sender, EventArgs e) |
186 | private void OnMqttServerAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e) |
|
168 | { |
187 | { |
|
169 | ActivityTextBox.AppendText( |
188 | ActivityTextBox.AppendText( |
|
170 | $"{Strings.Failed_to_authenticate_client}{Environment.NewLine}"); |
189 | $"{Strings.Failed_to_authenticate_client} : {e.Exception}{Environment.NewLine}"); |
|
171 | } |
190 | } |
|
172 | |
191 | |
|
173 | private void OnMqttClientAuthenticationFailed(object sender, EventArgs e) |
192 | private void OnMqttClientAuthenticationFailed(object sender, MqttAuthenticationFailureEventArgs e) |
|
174 | { |
193 | { |
|
175 | ActivityTextBox.AppendText( |
- | ||
176 | $"{Strings.Server_authentication_failed}{Environment.NewLine}"); |
- | ||
177 | } |
- | ||
178 | |
- | ||
179 | /// <summary> |
- | ||
180 | /// Clean up any resources being used. |
- | ||
181 | /// </summary> |
- | ||
182 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
- | ||
183 | protected override void Dispose(bool disposing) |
- | ||
184 | { |
- | ||
185 | if (disposing && components != null) |
- | ||
186 | { |
- | ||
187 | FormCancellationTokenSource.Dispose(); |
- | ||
188 | FormCancellationTokenSource = null; |
- | ||
189 | |
- | ||
190 | components.Dispose(); |
- | ||
191 | } |
- | ||
192 | |
194 | ActivityTextBox.AppendText( |
|
193 | base.Dispose(disposing); |
195 | $"{Strings.Failed_to_authenticate_with_server} : {e.Exception}{Environment.NewLine}"); |
|
194 | } |
196 | } |
|
195 | |
197 | |
|
196 | private void OnMouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e) |
198 | private void OnMouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e) |
|
197 | { |
199 | { |
|
198 | ActivityTextBox.AppendText( |
200 | ActivityTextBox.AppendText( |
|
199 | $"{Strings.Synchronized_bindings_with_client} : {e.Bindings.Nick} : {e.Bindings.MouseKeyBindings.Count}{Environment.NewLine}"); |
201 | $"{Strings.Synchronized_bindings_with_client} : {e.Bindings.Nick} : {e.Bindings.KeyBindings.Count}{Environment.NewLine}"); |
|
200 | |
202 | |
|
201 | var exchangeBindings = MouseKeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding => |
203 | var exchangeBindings = KeyBindingsExchange.ExchangeBindings.FirstOrDefault(exchangeBinding => |
|
202 | string.Equals(exchangeBinding.Nick, e.Bindings.Nick, StringComparison.Ordinal)); |
204 | string.Equals(exchangeBinding.Nick, e.Bindings.Nick, StringComparison.Ordinal)); |
|
203 | |
205 | |
|
204 | // If the nick does not exist then add it. |
206 | // If the nick does not exist then add it. |
|
205 | if (exchangeBindings == null) |
207 | if (exchangeBindings == null) |
|
206 | { |
208 | { |
|
207 | MouseKeyBindingsExchange.ExchangeBindings.Add(e.Bindings); |
209 | KeyBindingsExchange.ExchangeBindings.Add(e.Bindings); |
|
208 | RemoteBindingsComboBoxSource.ResetBindings(false); |
210 | RemoteBindingsComboBoxSource.ResetBindings(false); |
|
209 | UpdateRemoteListBoxItems(); |
211 | UpdateRemoteItems(); |
|
210 | return; |
212 | return; |
|
211 | } |
213 | } |
|
212 | |
214 | |
|
213 | // If the bindings for the nick have not changed then do not update. |
215 | // If the bindings for the nick have not changed then do not update. |
|
214 | if (exchangeBindings.MouseKeyBindings.SequenceEqual(e.Bindings.MouseKeyBindings)) |
216 | if (exchangeBindings.KeyBindings.SequenceEqual(e.Bindings.KeyBindings)) |
|
215 | { |
217 | { |
|
216 | RemoteBindingsComboBoxSource.ResetBindings(false); |
218 | RemoteBindingsComboBoxSource.ResetBindings(false); |
|
217 | UpdateRemoteListBoxItems(); |
219 | UpdateRemoteItems(); |
|
218 | return; |
220 | return; |
|
219 | } |
221 | } |
|
220 | |
222 | |
|
221 | // Update the bindings. |
223 | // Update the bindings. |
|
222 | exchangeBindings.MouseKeyBindings = e.Bindings.MouseKeyBindings; |
224 | exchangeBindings.KeyBindings = e.Bindings.KeyBindings; |
|
223 | RemoteBindingsComboBoxSource.ResetBindings(false); |
225 | RemoteBindingsComboBoxSource.ResetBindings(false); |
|
224 | UpdateRemoteListBoxItems(); |
226 | UpdateRemoteItems(); |
|
225 | } |
227 | } |
|
226 | |
228 | |
|
227 | private void UpdateRemoteListBoxItems() |
229 | private void UpdateRemoteItems() |
|
228 | { |
230 | { |
|
229 | var exchangeBinding = (List<MouseKeyBinding>) RemoteBindingsComboBox.SelectedValue; |
231 | var exchangeBindings = (List<KeyBinding>) RemoteBindingsComboBox.SelectedValue; |
|
230 | if (exchangeBinding == null) |
232 | if (exchangeBindings == null) |
|
- | 233 | return; |
||
- | 234 | |
||
- | 235 | var replaceMouseBindings = new List<RemoteKeyBinding>(); |
||
- | 236 | foreach (var remoteBinding in RemoteKeyBindings.Bindings) |
||
- | 237 | { |
||
- | 238 | if (!exchangeBindings.Any(binding => |
||
- | 239 | string.Equals(binding.Name, remoteBinding.Name, StringComparison.Ordinal))) |
||
- | 240 | continue; |
||
- | 241 | |
||
- | 242 | replaceMouseBindings.Add(remoteBinding); |
||
- | 243 | } |
||
- | 244 | |
||
231 | return; |
245 | RemoteKeyBindings.Bindings = replaceMouseBindings; |
|
232 | |
246 | |
|
233 | RemoteBindingsListBox.Items.Clear(); |
247 | RemoteBindingsListBox.Items.Clear(); |
|
234 | RemoteBindingsListBox.DisplayMember = "Name"; |
248 | RemoteBindingsListBox.DisplayMember = "Name"; |
|
235 | RemoteBindingsListBox.ValueMember = "Name"; |
249 | RemoteBindingsListBox.ValueMember = "Name"; |
|
236 | var i = exchangeBinding.Select(binding => (object) binding.Name).ToArray(); |
250 | var bindings = exchangeBindings.Select(binding => (object) binding.Name).ToArray(); |
|
237 | if (i.Length == 0) |
251 | if (bindings.Length == 0) |
|
238 | return; |
252 | return; |
|
239 | |
253 | |
|
240 | RemoteBindingsListBox.Items.AddRange(i); |
254 | RemoteBindingsListBox.Items.AddRange(bindings); |
|
241 | } |
255 | } |
|
242 | |
256 | |
|
243 | private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e) |
257 | private void OnLobbyMessageReceived(object sender, LobbyMessageReceivedEventArgs e) |
|
244 | { |
258 | { |
|
245 | LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}"); |
259 | LobbyTextBox.AppendText($"{e.Nick} : {e.Message}{Environment.NewLine}"); |
|
246 | } |
260 | } |
|
247 | |
261 | |
|
248 | private void AddressTextBoxClick(object sender, EventArgs e) |
262 | private void AddressTextBoxClick(object sender, EventArgs e) |
|
249 | { |
263 | { |
|
250 | Address.BackColor = Color.Empty; |
264 | Address.BackColor = Color.Empty; |
|
251 | } |
265 | } |
|
252 | |
266 | |
|
253 | private void PortTextBoxClick(object sender, EventArgs e) |
267 | private void PortTextBoxClick(object sender, EventArgs e) |
|
254 | { |
268 | { |
|
255 | Port.BackColor = Color.Empty; |
269 | Port.BackColor = Color.Empty; |
|
256 | } |
270 | } |
|
257 | |
271 | |
|
258 | private async void HostButtonClickAsync(object sender, EventArgs e) |
272 | private async void HostButtonClickAsync(object sender, EventArgs e) |
|
259 | { |
273 | { |
|
260 | // Stop the MQTT server if it is running. |
274 | // Stop the MQTT server if it is running. |
|
261 | if (MqttCommunication.Running) |
275 | if (MqttCommunication.Running) |
|
262 | { |
276 | { |
|
263 | await MqttCommunication.Stop().ConfigureAwait(false); |
277 | await MqttCommunication.Stop(); |
|
264 | HostButton.BackColor = Color.Empty; |
278 | HostButton.BackColor = Color.Empty; |
|
265 | |
279 | |
|
266 | // Enable controls. |
280 | // Enable controls. |
|
267 | ConnectButton.Enabled = true; |
281 | ConnectButton.Enabled = true; |
|
268 | Address.Enabled = true; |
282 | Address.Enabled = true; |
|
269 | Port.Enabled = true; |
283 | Port.Enabled = true; |
|
270 | Nick.Enabled = true; |
284 | Nick.Enabled = true; |
|
271 | |
- | ||
- | 285 | Password.Enabled = true; |
||
272 | return; |
286 | return; |
|
273 | } |
287 | } |
|
274 | |
288 | |
|
275 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
289 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
|
276 | return; |
290 | return; |
|
277 | |
291 | |
|
278 | // Start the MQTT server. |
292 | // Start the MQTT server. |
|
279 | if (!await MqttCommunication.Start(MqttCommunicationType.Server, ipAddress, port, nick, password) |
293 | if (!await MqttCommunication |
|
280 | .ConfigureAwait(false)) |
294 | .Start(MqttCommunicationType.Server, ipAddress, port, nick, password)) |
|
281 | { |
295 | { |
|
282 | ActivityTextBox.AppendText( |
296 | ActivityTextBox.AppendText( |
|
283 | $"{Strings.Failed_starting_server}{Environment.NewLine}"); |
297 | $"{Strings.Failed_starting_server}{Environment.NewLine}"); |
|
284 | return; |
298 | return; |
|
285 | } |
299 | } |
|
286 | |
300 | |
|
287 | HostButton.BackColor = Color.Aquamarine; |
301 | HostButton.BackColor = Color.Aquamarine; |
|
288 | |
302 | |
|
289 | // Disable controls |
303 | // Disable controls |
|
290 | ConnectButton.Enabled = false; |
304 | ConnectButton.Enabled = false; |
|
291 | Address.Enabled = false; |
305 | Address.Enabled = false; |
|
292 | Port.Enabled = false; |
306 | Port.Enabled = false; |
|
293 | Nick.Enabled = false; |
307 | Nick.Enabled = false; |
|
- | 308 | Password.Enabled = false; |
||
294 | } |
309 | } |
|
295 | |
310 | |
|
296 | private bool ValidateAddressPort(out IPAddress address, out int port, out string nick, out string password) |
311 | private bool ValidateAddressPort(out IPAddress address, out int port, out string nick, out string password) |
|
297 | { |
312 | { |
|
298 | address = IPAddress.Any; |
313 | address = IPAddress.Any; |
|
299 | port = 0; |
314 | port = 0; |
|
300 | nick = string.Empty; |
315 | nick = string.Empty; |
|
301 | password = string.Empty; |
316 | password = string.Empty; |
|
302 | |
317 | |
|
303 | if (string.IsNullOrEmpty(Address.Text) && |
318 | if (string.IsNullOrEmpty(Address.Text) && |
|
304 | string.IsNullOrEmpty(Port.Text) && |
319 | string.IsNullOrEmpty(Port.Text) && |
|
305 | string.IsNullOrEmpty(Nick.Text) && |
320 | string.IsNullOrEmpty(Nick.Text) && |
|
306 | string.IsNullOrEmpty(Password.Text)) |
321 | string.IsNullOrEmpty(Password.Text)) |
|
307 | { |
322 | { |
|
308 | Address.BackColor = Color.LightPink; |
323 | Address.BackColor = Color.LightPink; |
|
309 | Port.BackColor = Color.LightPink; |
324 | Port.BackColor = Color.LightPink; |
|
310 | Nick.BackColor = Color.LightPink; |
325 | Nick.BackColor = Color.LightPink; |
|
311 | Password.BackColor = Color.LightPink; |
326 | Password.BackColor = Color.LightPink; |
|
312 | return false; |
327 | return false; |
|
313 | } |
328 | } |
|
314 | |
329 | |
|
315 | if (!IPAddress.TryParse(Address.Text, out address)) |
330 | if (!IPAddress.TryParse(Address.Text, out address)) |
|
316 | { |
331 | { |
|
317 | Address.BackColor = Color.LightPink; |
332 | Address.BackColor = Color.LightPink; |
|
318 | return false; |
333 | return false; |
|
319 | } |
334 | } |
|
320 | |
335 | |
|
321 | if (!uint.TryParse(Port.Text, out var uPort)) |
336 | if (!uint.TryParse(Port.Text, out var uPort)) |
|
322 | { |
337 | { |
|
323 | Port.BackColor = Color.LightPink; |
338 | Port.BackColor = Color.LightPink; |
|
324 | return false; |
339 | return false; |
|
325 | } |
340 | } |
|
326 | |
341 | |
|
327 | port = (int) uPort; |
342 | port = (int) uPort; |
|
328 | |
343 | |
|
329 | if (string.IsNullOrEmpty(Nick.Text)) |
344 | if (string.IsNullOrEmpty(Nick.Text)) |
|
330 | { |
345 | { |
|
331 | Nick.BackColor = Color.LightPink; |
346 | Nick.BackColor = Color.LightPink; |
|
332 | return false; |
347 | return false; |
|
333 | } |
348 | } |
|
334 | |
349 | |
|
335 | nick = Nick.Text; |
350 | nick = Nick.Text; |
|
336 | |
351 | |
|
337 | if (string.IsNullOrEmpty(Password.Text)) |
352 | if (string.IsNullOrEmpty(Password.Text)) |
|
338 | { |
353 | { |
|
339 | Password.BackColor = Color.LightPink; |
354 | Password.BackColor = Color.LightPink; |
|
340 | return false; |
355 | return false; |
|
341 | } |
356 | } |
|
342 | |
357 | |
|
343 | password = AES.LinearFeedbackShiftPassword(Password.Text); |
358 | password = AES.ExpandKey(Password.Text); |
|
344 | |
359 | |
|
345 | Address.BackColor = Color.Empty; |
360 | Address.BackColor = Color.Empty; |
|
346 | Port.BackColor = Color.Empty; |
361 | Port.BackColor = Color.Empty; |
|
347 | Nick.BackColor = Color.Empty; |
362 | Nick.BackColor = Color.Empty; |
|
348 | Password.BackColor = Color.Empty; |
363 | Password.BackColor = Color.Empty; |
|
349 | |
364 | |
|
350 | return true; |
365 | return true; |
|
351 | } |
366 | } |
|
352 | |
367 | |
|
353 | private async void ConnectButtonClickAsync(object sender, EventArgs e) |
368 | private async void ConnectButtonClickAsync(object sender, EventArgs e) |
|
354 | { |
369 | { |
|
355 | if (MqttCommunication.Running) |
370 | if (MqttCommunication.Running) |
|
356 | { |
371 | { |
|
357 | await MqttCommunication.Stop().ConfigureAwait(false); |
372 | await MqttCommunication.Stop(); |
|
358 | ConnectButton.Text = Strings.Connect; |
373 | ConnectButton.Text = Strings.Connect; |
|
359 | ConnectButton.BackColor = Color.Empty; |
374 | ConnectButton.BackColor = Color.Empty; |
|
360 | |
375 | |
|
361 | Address.Enabled = true; |
376 | Address.Enabled = true; |
|
362 | Port.Enabled = true; |
377 | Port.Enabled = true; |
|
363 | Nick.Enabled = true; |
378 | Nick.Enabled = true; |
|
- | 379 | Password.Enabled = true; |
||
364 | HostButton.Enabled = true; |
380 | HostButton.Enabled = true; |
|
365 | return; |
381 | return; |
|
366 | } |
382 | } |
|
367 | |
383 | |
|
368 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
384 | if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
|
369 | return; |
385 | return; |
|
370 | |
386 | |
|
371 | if (!await MqttCommunication.Start(MqttCommunicationType.Client, ipAddress, port, nick, password) |
387 | if (!await MqttCommunication |
|
372 | .ConfigureAwait(false)) |
388 | .Start(MqttCommunicationType.Client, ipAddress, port, nick, password)) |
|
373 | { |
389 | { |
|
374 | ActivityTextBox.AppendText( |
390 | ActivityTextBox.AppendText( |
|
375 | $"{Strings.Failed_starting_client}{Environment.NewLine}"); |
391 | $"{Strings.Failed_starting_client}{Environment.NewLine}"); |
|
376 | return; |
392 | return; |
|
377 | } |
393 | } |
|
378 | |
394 | |
|
379 | ConnectButton.Text = Strings.Disconnect; |
395 | ConnectButton.Text = Strings.Disconnect; |
|
380 | ConnectButton.BackColor = Color.Aquamarine; |
396 | ConnectButton.BackColor = Color.Aquamarine; |
|
381 | |
397 | |
|
382 | HostButton.Enabled = false; |
398 | HostButton.Enabled = false; |
|
383 | Address.Enabled = false; |
399 | Address.Enabled = false; |
|
384 | Port.Enabled = false; |
400 | Port.Enabled = false; |
|
385 | Nick.Enabled = false; |
401 | Nick.Enabled = false; |
|
- | 402 | Password.Enabled = false; |
||
386 | } |
403 | } |
|
387 | |
404 | |
|
388 | private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) |
405 | private async void LobbySayTextBoxKeyDown(object sender, KeyEventArgs e) |
|
389 | { |
406 | { |
|
390 | if (e.KeyCode != Keys.Enter) |
407 | if (e.KeyCode != Keys.Enter) |
|
391 | return; |
408 | return; |
|
392 | |
409 | |
|
393 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text).ConfigureAwait(false); |
410 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); |
|
394 | |
411 | |
|
395 | LobbySayTextBox.Text = string.Empty; |
412 | LobbySayTextBox.Text = string.Empty; |
|
396 | } |
413 | } |
|
397 | |
414 | |
|
398 | private void HelmAddButtonClick(object sender, EventArgs e) |
415 | private void LocalAddBindingButtonClick(object sender, EventArgs e) |
|
399 | { |
416 | { |
|
400 | if (string.IsNullOrEmpty(LocalNameTextBox.Text)) |
417 | if (string.IsNullOrEmpty(LocalNameTextBox.Text)) |
|
401 | { |
418 | { |
|
402 | LocalNameTextBox.BackColor = Color.LightPink; |
419 | LocalNameTextBox.BackColor = Color.LightPink; |
|
403 | return; |
420 | return; |
|
404 | } |
421 | } |
|
- | 422 | |
||
- | 423 | // Only unique names allowed. |
||
- | 424 | if (LocalKeyBindings.Bindings.Any(binding => |
||
- | 425 | string.Equals(binding.Name, LocalNameTextBox.Text, StringComparison.Ordinal))) |
||
- | 426 | { |
||
- | 427 | LocalNameTextBox.BackColor = Color.LightPink; |
||
- | 428 | LocalBindingsListBox.BackColor = Color.LightPink; |
||
- | 429 | return; |
||
- | 430 | } |
||
- | 431 | |
||
- | 432 | LocalNameTextBox.BackColor = Color.Empty; |
||
- | 433 | LocalBindingsListBox.BackColor = Color.Empty; |
||
405 | |
434 | |
|
406 | ShowOverlayPanel(); |
435 | ShowOverlayPanel(); |
|
407 | |
436 | |
|
408 | MouseKeyCombo = new List<string>(); |
437 | MouseKeyCombo = new List<string>(); |
|
409 | |
438 | |
|
410 | MouseKeyApplicationHook = Hook.AppEvents(); |
- | ||
411 | MouseKeyApplicationHook.MouseDown += LocalMouseKeyHookOnMouseDown; |
439 | MouseKeyApplicationHook = Hook.GlobalEvents(); |
|
412 | MouseKeyApplicationHook.KeyUp += LocalMouseKeyHookOnKeyUp; |
440 | MouseKeyApplicationHook.KeyUp += LocalMouseKeyHookOnKeyUp; |
|
413 | MouseKeyApplicationHook.KeyDown += LocalMouseKeyHookOnKeyDown; |
- | ||
414 | MouseKeyApplicationHook.MouseUp += LocalMouseKeyHookOnMouseUp; |
441 | MouseKeyApplicationHook.KeyDown += LocalMouseKeyHookOnKeyDown; |
|
415 | } |
442 | } |
|
416 | |
443 | |
|
417 | private void ShowOverlayPanel() |
444 | private void ShowOverlayPanel() |
|
418 | { |
445 | { |
|
419 | OverlayPanel.BringToFront(); |
446 | OverlayPanel.BringToFront(); |
|
420 | OverlayPanel.Visible = true; |
447 | OverlayPanel.Visible = true; |
|
421 | OverlayPanel.Invalidate(); |
448 | OverlayPanel.Invalidate(); |
|
422 | } |
449 | } |
|
423 | |
450 | |
|
424 | private async void LocalMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
451 | private void LocalMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
|
425 | { |
452 | { |
|
426 | LocalMouseKeyBindings.Bindings.Add(new MouseKeyBinding(LocalNameTextBox.Text, MouseKeyCombo)); |
453 | LocalKeyBindings.Bindings.Add(new KeyBinding(LocalNameTextBox.Text, MouseKeyCombo)); |
|
427 | |
454 | |
|
428 | LocalListBoxBindingSource.ResetBindings(false); |
455 | LocalListBoxBindingSource.ResetBindings(false); |
|
429 | |
456 | |
|
430 | MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; |
457 | MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; |
|
431 | MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; |
458 | MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; |
|
432 | MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; |
- | ||
433 | MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; |
- | ||
434 | |
459 | |
|
435 | MouseKeyApplicationHook.Dispose(); |
460 | MouseKeyApplicationHook.Dispose(); |
|
436 | |
461 | |
|
437 | LocalNameTextBox.Text = string.Empty; |
462 | LocalNameTextBox.Text = string.Empty; |
|
438 | HideOverlayPanel(); |
463 | HideOverlayPanel(); |
|
439 | |
464 | |
|
440 | await SaveLocalMouseKeyBindings().ConfigureAwait(false); |
465 | //await SaveLocalMouseKeyBindings(); |
|
441 | } |
466 | } |
|
442 | |
467 | |
|
443 | private void HideOverlayPanel() |
468 | private void HideOverlayPanel() |
|
444 | { |
469 | { |
|
445 | OverlayPanel.SendToBack(); |
470 | OverlayPanel.SendToBack(); |
|
446 | OverlayPanel.Visible = false; |
471 | OverlayPanel.Visible = false; |
|
447 | OverlayPanel.Invalidate(); |
472 | OverlayPanel.Invalidate(); |
|
448 | } |
473 | } |
|
449 | |
- | ||
450 | private async void LocalMouseKeyHookOnMouseUp(object sender, MouseEventArgs e) |
- | ||
451 | { |
- | ||
452 | LocalMouseKeyBindings.Bindings.Add(new MouseKeyBinding(LocalNameTextBox.Text, MouseKeyCombo)); |
- | ||
453 | |
- | ||
454 | LocalListBoxBindingSource.ResetBindings(false); |
- | ||
455 | |
- | ||
456 | MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; |
- | ||
457 | MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; |
- | ||
458 | MouseKeyApplicationHook.KeyDown -= LocalMouseKeyHookOnKeyDown; |
- | ||
459 | MouseKeyApplicationHook.KeyUp -= LocalMouseKeyHookOnKeyUp; |
- | ||
460 | |
- | ||
461 | MouseKeyApplicationHook.Dispose(); |
- | ||
462 | |
- | ||
463 | LocalNameTextBox.Text = string.Empty; |
- | ||
464 | HideOverlayPanel(); |
- | ||
465 | |
- | ||
466 | await SaveLocalMouseKeyBindings().ConfigureAwait(false); |
- | ||
467 | } |
- | ||
468 | |
- | ||
469 | |
- | ||
470 | private void LocalMouseKeyHookOnMouseDown(object sender, MouseEventArgs e) |
- | ||
471 | { |
- | ||
472 | MouseKeyCombo.Add(e.Button.ToDisplayName()); |
- | ||
473 | } |
- | ||
474 | |
474 | |
|
475 | private void LocalMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
475 | private void LocalMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
|
476 | { |
476 | { |
|
477 | e.SuppressKeyPress = true; |
477 | e.SuppressKeyPress = true; |
|
- | 478 | |
||
- | 479 | KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key); |
||
478 | |
480 | |
|
479 | MouseKeyCombo.Add(e.KeyCode.ToDisplayName()); |
481 | MouseKeyCombo.Add(key); |
|
480 | } |
482 | } |
|
481 | |
483 | |
|
482 | private void HelmNameTextBoxClick(object sender, EventArgs e) |
484 | private void LocalNameTextBoxClick(object sender, EventArgs e) |
|
483 | { |
485 | { |
|
484 | LocalNameTextBox.BackColor = Color.Empty; |
486 | LocalNameTextBox.BackColor = Color.Empty; |
|
485 | } |
487 | } |
|
486 | |
488 | |
|
487 | private async void HelmRemoveButtonClick(object sender, EventArgs e) |
489 | private void LocalBindingsRemoveButtonClick(object sender, EventArgs e) |
|
488 | { |
490 | { |
|
489 | var helmBinding = (MouseKeyBinding) LocalBindingsListBox.SelectedItem; |
491 | var helmBinding = (KeyBinding) LocalBindingsListBox.SelectedItem; |
|
490 | if (helmBinding == null) |
492 | if (helmBinding == null) |
|
491 | return; |
493 | return; |
|
492 | |
494 | |
|
493 | LocalMouseKeyBindings.Bindings.Remove(helmBinding); |
495 | LocalKeyBindings.Bindings.Remove(helmBinding); |
|
494 | LocalListBoxBindingSource.ResetBindings(false); |
496 | LocalListBoxBindingSource.ResetBindings(false); |
|
495 | |
497 | |
|
496 | await SaveLocalMouseKeyBindings().ConfigureAwait(false); |
498 | // await SaveLocalMouseKeyBindings(); |
|
497 | } |
499 | } |
|
498 | |
500 | |
|
499 | private async void LobbySayButtonClick(object sender, EventArgs e) |
501 | private async void LobbySayButtonClick(object sender, EventArgs e) |
|
500 | { |
502 | { |
|
501 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text).ConfigureAwait(false); |
503 | await LobbyMessageSynchronizer.Broadcast(LobbySayTextBox.Text); |
|
502 | |
504 | |
|
503 | LobbySayTextBox.Text = string.Empty; |
505 | LobbySayTextBox.Text = string.Empty; |
|
504 | } |
506 | } |
|
505 | |
507 | |
|
506 | private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) |
508 | private void RemoteBindingsComboBoxSelectionChangeCompleted(object sender, EventArgs e) |
|
507 | { |
509 | { |
|
508 | UpdateRemoteListBoxItems(); |
510 | UpdateRemoteItems(); |
|
509 | } |
511 | } |
|
510 | |
512 | |
|
511 | private async void WingManFormOnLoad(object sender, EventArgs e) |
513 | private void WingManFormOnLoad(object sender, EventArgs e) |
|
512 | { |
514 | { |
|
- | 515 | // await LoadLocalMouseKeyBindings(); |
||
513 | await LoadLocalMouseKeyBindings(); |
516 | |
|
514 | await LoadRemoteMouseKeyBindings(); |
517 | // await LoadRemoteMouseKeyBindings(); |
|
515 | } |
518 | } |
|
516 | |
- | ||
517 | private async Task LoadLocalMouseKeyBindings() |
- | ||
518 | { |
519 | |
|
519 | try |
520 | private void RemoteBindingsBindButtonClicked(object sender, EventArgs e) |
|
520 | { |
521 | { |
|
521 | using (var fileStream = new FileStream("LocalMouseKeyBindings.xml", FileMode.Open)) |
522 | if (string.IsNullOrEmpty((string) RemoteBindingsListBox.SelectedItem)) |
|
522 | { |
523 | { |
|
523 | using (var memoryStream = new MemoryStream()) |
524 | RemoteBindingsListBox.BackColor = Color.LightPink; |
|
524 | { |
525 | return; |
|
525 | await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false); |
526 | } |
|
526 | |
- | ||
527 | memoryStream.Position = 0L; |
527 | |
|
528 | |
528 | RemoteBindingsListBox.BackColor = Color.Empty; |
|
529 | var loadedBindings = |
529 | |
|
530 | (MouseKeyBindings) MouseKeyBindings.XmlSerializer.Deserialize(memoryStream); |
530 | ShowOverlayPanel(); |
|
531 | |
531 | |
|
- | 532 | MouseKeyCombo = new List<string>(); |
||
532 | foreach (var binding in loadedBindings.Bindings) LocalMouseKeyBindings.Bindings.Add(binding); |
533 | |
|
533 | |
534 | MouseKeyApplicationHook = Hook.GlobalEvents(); |
|
534 | LocalListBoxBindingSource.ResetBindings(false); |
535 | MouseKeyApplicationHook.KeyUp += RemoteMouseKeyHookOnKeyUp; |
|
535 | } |
536 | MouseKeyApplicationHook.KeyDown += RemoteMouseKeyHookOnKeyDown; |
|
536 | } |
537 | } |
|
- | 538 | |
||
- | 539 | private void RemoteBindingsUnbindButtonClicked(object sender, EventArgs e) |
||
537 | } |
540 | { |
|
538 | catch (Exception) |
541 | var item = (string) RemoteBindingsListBox.SelectedItem; |
|
539 | { |
- | ||
540 | ActivityTextBox.AppendText( |
- | ||
- | 542 | if (string.IsNullOrEmpty(item)) |
||
541 | $"{Strings.Failed_loading_local_bindings}{Environment.NewLine}"); |
543 | { |
|
542 | } |
544 | RemoteBindingsListBox.BackColor = Color.LightPink; |
|
543 | } |
- | ||
544 | |
545 | return; |
|
- | 546 | } |
||
545 | private void RemoteBindingsBindButtonClicked(object sender, EventArgs e) |
547 | |
|
546 | { |
548 | RemoteBindingsListBox.BackColor = Color.Empty; |
|
547 | ShowOverlayPanel(); |
549 | |
|
548 | |
550 | var remoteKeyBinding = RemoteKeyBindings.Bindings.FirstOrDefault(binding => |
|
549 | MouseKeyCombo = new List<string>(); |
551 | string.Equals(binding.Name, item, StringComparison.Ordinal)); |
|
550 | |
552 | |
|
551 | MouseKeyApplicationHook = Hook.AppEvents(); |
553 | if (remoteKeyBinding == null) |
|
552 | MouseKeyApplicationHook.MouseDown += RemoteMouseKeyHookOnMouseDown; |
554 | return; |
|
553 | MouseKeyApplicationHook.KeyUp += RemoteMouseKeyHookOnKeyUp; |
555 | |
|
554 | MouseKeyApplicationHook.KeyDown += RemoteMouseKeyHookOnKeyDown; |
556 | RemoteKeyBindings.Bindings.Remove(remoteKeyBinding); |
|
555 | MouseKeyApplicationHook.MouseUp += RemoteMouseKeyHookOnMouseUp; |
557 | RemoteBindingsBindToBox.Text = string.Empty; |
|
556 | } |
558 | } |
|
557 | |
559 | |
|
558 | private void RemoteMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
560 | private void RemoteMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
|
559 | { |
561 | { |
|
560 | e.SuppressKeyPress = true; |
562 | e.SuppressKeyPress = true; |
|
561 | |
563 | |
|
562 | MouseKeyCombo.Add(e.KeyCode.ToDisplayName()); |
- | ||
563 | } |
- | ||
564 | |
- | ||
565 | private void RemoteMouseKeyHookOnMouseDown(object sender, MouseEventArgs e) |
564 | KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key); |
|
566 | { |
565 | |
|
567 | MouseKeyCombo.Add(e.Button.ToDisplayName()); |
566 | MouseKeyCombo.Add(key); |
|
568 | } |
567 | } |
|
569 | |
568 | |
|
570 | private async void RemoteMouseKeyHookOnMouseUp(object sender, MouseEventArgs e) |
569 | private void RemoteMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
|
571 | { |
570 | { |
|
572 | RemoteMouseKeyBindings.Bindings.Add(new RemoteMouseKeyBinding(RemoteBindingsComboBox.Text, |
571 | RemoteKeyBindings.Bindings.Add(new RemoteKeyBinding(RemoteBindingsComboBox.Text, |
|
573 | (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo)); |
572 | (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo)); |
|
574 | |
573 | |
|
575 | MouseKeyApplicationHook.KeyDown -= RemoteMouseKeyHookOnKeyDown; |
574 | MouseKeyApplicationHook.KeyDown -= RemoteMouseKeyHookOnKeyDown; |
|
576 | MouseKeyApplicationHook.KeyUp -= RemoteMouseKeyHookOnKeyUp; |
575 | MouseKeyApplicationHook.KeyUp -= RemoteMouseKeyHookOnKeyUp; |
|
577 | MouseKeyApplicationHook.KeyDown -= RemoteMouseKeyHookOnKeyDown; |
- | ||
578 | MouseKeyApplicationHook.KeyUp -= RemoteMouseKeyHookOnKeyUp; |
- | ||
579 | |
576 | |
|
580 | MouseKeyApplicationHook.Dispose(); |
577 | MouseKeyApplicationHook.Dispose(); |
|
581 | |
578 | |
|
582 | RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo); |
579 | RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo); |
|
583 | HideOverlayPanel(); |
580 | HideOverlayPanel(); |
|
584 | |
581 | |
|
585 | await SaveRemoteMouseKeyBindings().ConfigureAwait(false); |
582 | // await SaveRemoteMouseKeyBindings(); |
|
586 | } |
583 | } |
|
587 | |
584 | |
|
588 | private async void RemoteMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
- | ||
589 | { |
585 | private void RemoteBindingsListBoxSelectedValueChanged(object sender, EventArgs e) |
|
590 | RemoteMouseKeyBindings.Bindings.Add(new RemoteMouseKeyBinding(RemoteBindingsComboBox.Text, |
586 | { |
|
591 | (string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo)); |
587 | RemoteBindingsBindToBox.Text = ""; |
|
592 | |
- | ||
593 | MouseKeyApplicationHook.KeyDown -= RemoteMouseKeyHookOnKeyDown; |
588 | |
|
- | 589 | var name = (string) RemoteBindingsListBox.SelectedItem; |
||
- | 590 | if (string.IsNullOrEmpty(name)) |
||
- | 591 | return; |
||
594 | MouseKeyApplicationHook.KeyUp -= RemoteMouseKeyHookOnKeyUp; |
592 | |
|
595 | MouseKeyApplicationHook.KeyDown -= RemoteMouseKeyHookOnKeyDown; |
593 | foreach (var binding in RemoteKeyBindings.Bindings) |
|
596 | MouseKeyApplicationHook.KeyUp -= RemoteMouseKeyHookOnKeyUp; |
594 | { |
|
- | 595 | if (!string.Equals(binding.Name, name)) |
||
- | 596 | continue; |
||
- | 597 | |
||
- | 598 | RemoteBindingsBindToBox.Text = string.Join(" + ", binding.Keys); |
||
- | 599 | break; |
||
- | 600 | } |
||
- | 601 | } |
||
- | 602 | |
||
- | 603 | #region Saving and loading |
||
- | 604 | |
||
- | 605 | private async Task SaveLocalMouseKeyBindings() |
||
- | 606 | { |
||
- | 607 | try |
||
- | 608 | { |
||
- | 609 | using (var memoryStream = new MemoryStream()) |
||
- | 610 | { |
||
- | 611 | LocalKeyBindings.XmlSerializer.Serialize(memoryStream, LocalKeyBindings); |
||
- | 612 | |
||
- | 613 | memoryStream.Position = 0L; |
||
- | 614 | |
||
- | 615 | using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Create)) |
||
- | 616 | { |
||
- | 617 | await memoryStream.CopyToAsync(fileStream); |
||
- | 618 | } |
||
- | 619 | } |
||
- | 620 | } |
||
- | 621 | catch (Exception) |
||
- | 622 | { |
||
- | 623 | ActivityTextBox.AppendText( |
||
- | 624 | $"{Strings.Failed_saving_local_bindings}{Environment.NewLine}"); |
||
- | 625 | } |
||
- | 626 | } |
||
- | 627 | |
||
- | 628 | private async Task LoadLocalMouseKeyBindings() |
||
- | 629 | { |
||
- | 630 | try |
||
- | 631 | { |
||
- | 632 | using (var fileStream = new FileStream("LocalKeyBindings.xml", FileMode.Open)) |
||
- | 633 | { |
||
- | 634 | using (var memoryStream = new MemoryStream()) |
||
- | 635 | { |
||
- | 636 | await fileStream.CopyToAsync(memoryStream); |
||
- | 637 | |
||
- | 638 | memoryStream.Position = 0L; |
||
597 | |
639 | |
|
- | 640 | var loadedBindings = |
||
- | 641 | (LocalKeyBindings) LocalKeyBindings.XmlSerializer.Deserialize(memoryStream); |
||
- | 642 | |
||
- | 643 | foreach (var binding in loadedBindings.Bindings) LocalKeyBindings.Bindings.Add(binding); |
||
- | 644 | |
||
- | 645 | LocalListBoxBindingSource.ResetBindings(false); |
||
- | 646 | } |
||
- | 647 | } |
||
598 | MouseKeyApplicationHook.Dispose(); |
648 | } |
|
599 | |
649 | catch (Exception) |
|
600 | RemoteBindingsBindToBox.Text = string.Join(" + ", MouseKeyCombo); |
650 | { |
|
601 | HideOverlayPanel(); |
651 | ActivityTextBox.AppendText( |
|
602 | |
652 | $"{Strings.Failed_loading_local_bindings}{Environment.NewLine}"); |
|
603 | await SaveRemoteMouseKeyBindings().ConfigureAwait(false); |
653 | } |
|
604 | } |
654 | } |
|
605 | |
655 | |
|
606 | private async Task SaveRemoteMouseKeyBindings() |
656 | private async Task SaveRemoteMouseKeyBindings() |
|
607 | { |
657 | { |
|
608 | try |
658 | try |
|
609 | { |
659 | { |
|
610 | using (var memoryStream = new MemoryStream()) |
660 | using (var memoryStream = new MemoryStream()) |
|
611 | { |
661 | { |
|
612 | RemoteMouseKeyBindings.XmlSerializer.Serialize(memoryStream, RemoteMouseKeyBindings); |
662 | RemoteKeyBindings.XmlSerializer.Serialize(memoryStream, RemoteKeyBindings); |
|
613 | |
663 | |
|
614 | memoryStream.Position = 0L; |
664 | memoryStream.Position = 0L; |
|
615 | |
665 | |
|
616 | using (var fileStream = new FileStream("RemoteMouseKeyBindings.xml", FileMode.Create)) |
666 | using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Create)) |
|
617 | { |
667 | { |
|
618 | await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false); |
668 | await memoryStream.CopyToAsync(fileStream); |
|
619 | } |
669 | } |
|
620 | } |
670 | } |
|
621 | } |
671 | } |
|
622 | catch (Exception) |
672 | catch (Exception) |
|
623 | { |
673 | { |
|
624 | ActivityTextBox.AppendText( |
674 | ActivityTextBox.AppendText( |
|
625 | $"{Strings.Failed_saving_remote_bindings}{Environment.NewLine}"); |
675 | $"{Strings.Failed_saving_remote_bindings}{Environment.NewLine}"); |
|
626 | } |
676 | } |
|
627 | } |
677 | } |
|
628 | |
678 | |
|
629 | private async Task LoadRemoteMouseKeyBindings() |
679 | private async Task LoadRemoteMouseKeyBindings() |
|
630 | { |
680 | { |
|
631 | try |
681 | try |
|
632 | { |
682 | { |
|
633 | using (var fileStream = new FileStream("RemoteMouseKeyBindings.xml", FileMode.Open)) |
683 | using (var fileStream = new FileStream("RemoteKeyBindings.xml", FileMode.Open)) |
|
634 | { |
684 | { |
|
635 | using (var memoryStream = new MemoryStream()) |
685 | using (var memoryStream = new MemoryStream()) |
|
636 | { |
686 | { |
|
637 | await fileStream.CopyToAsync(memoryStream).ConfigureAwait(false); |
687 | await fileStream.CopyToAsync(memoryStream); |
|
638 | |
688 | |
|
639 | memoryStream.Position = 0L; |
689 | memoryStream.Position = 0L; |
|
640 | |
690 | |
|
641 | var loadedBindings = |
691 | var loadedBindings = |
|
642 | (RemoteMouseKeyBindings) RemoteMouseKeyBindings.XmlSerializer.Deserialize(memoryStream); |
692 | (RemoteKeyBindings) RemoteKeyBindings.XmlSerializer.Deserialize(memoryStream); |
|
643 | |
693 | |
|
644 | foreach (var binding in loadedBindings.Bindings) RemoteMouseKeyBindings.Bindings.Add(binding); |
694 | foreach (var binding in loadedBindings.Bindings) RemoteKeyBindings.Bindings.Add(binding); |
|
645 | } |
695 | } |
|
646 | } |
696 | } |
|
647 | } |
697 | } |
|
648 | catch (Exception) |
698 | catch (Exception) |
|
649 | { |
699 | { |
|
650 | ActivityTextBox.AppendText( |
700 | ActivityTextBox.AppendText( |
|
651 | $"{Strings.Failed_loading_remote_bindings}{Environment.NewLine}"); |
701 | $"{Strings.Failed_loading_remote_bindings}{Environment.NewLine}"); |
|
652 | } |
702 | } |
|
653 | } |
703 | } |
|
- | 704 | |
||
- | 705 | #endregion |
||
654 | } |
706 | } |
|
655 | } |
707 | } |
|
656 | |
708 | |