WingMan – Blame information for rev 9

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