WingMan – Blame information for rev 18

Subversion Repositories:
Rev:
Rev Author Line No. Line
10 office 1 using System;
2 using System.IO;
3 using System.Threading;
4 using System.Threading.Tasks;
5 using MQTTnet;
6 using SimWinInput;
7 using WingMan.Communication;
14 office 8 using WingMan.Utilities;
10 office 9  
14 office 10 namespace WingMan.Bindings
10 office 11 {
12 public class KeySimulator : IDisposable
13 {
14 public delegate void MouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args);
15  
16 public KeySimulator(LocalKeyBindings localLocalKeyBindings, MqttCommunication mqttCommunication,
17 TaskScheduler formTaskScheduler, CancellationToken cancellationToken)
18 {
19 LocalLocalKeyBindings = localLocalKeyBindings;
20 MqttCommunication = mqttCommunication;
21 TaskScheduler = formTaskScheduler;
22 CancellationToken = cancellationToken;
23  
24 MqttCommunication.OnMessageReceived += OnMqttMessageReceived;
25 }
26  
27 private MqttCommunication MqttCommunication { get; }
28 private TaskScheduler TaskScheduler { get; }
29 private CancellationToken CancellationToken { get; }
30 private LocalKeyBindings LocalLocalKeyBindings { get; }
31  
32 public void Dispose()
33 {
34 MqttCommunication.OnMessageReceived -= OnMqttMessageReceived;
35 }
36  
37 public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting;
38  
39 private async void OnMqttMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
40 {
41 if (e.ApplicationMessage.Topic != "execute")
42 return;
43  
44 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
45 {
46 var executeMouseKeyBinding =
47 (ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream);
48  
49 // Do not process own mouse key bindings.
50 if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal))
51 return;
52  
53 await Task.Delay(0, CancellationToken)
54 .ContinueWith(
55 _ => OnMouseKeyBindingExecuting?.Invoke(sender,
56 new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick,
57 executeMouseKeyBinding.Name)),
58 CancellationToken,
59 TaskContinuationOptions.None, TaskScheduler);
60  
61 Simulate(executeMouseKeyBinding);
62 }
63 }
64  
18 office 65 private async void Simulate(ExecuteKeyBinding executeBinding)
10 office 66 {
67 foreach (var localBinding in LocalLocalKeyBindings.Bindings)
68 {
69 if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal))
70 continue;
71  
72 // Press
73 foreach (var key in localBinding.Keys)
74 {
14 office 75 if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey))
10 office 76 continue;
77  
14 office 78 SimKeyboard.KeyDown(pressKey);
10 office 79 }
80  
18 office 81 await Task.Delay(250);
82  
10 office 83 // Depress
84 foreach (var key in localBinding.Keys)
85 {
14 office 86 if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey))
10 office 87 continue;
88  
14 office 89 SimKeyboard.KeyUp(pressKey);
10 office 90 }
91 }
92 }
93 }
94 }