WingMan – Blame information for rev 35

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;
19 office 5 using WindowsInput;
6 using WindowsInput.Native;
10 office 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;
19 office 25  
26 InputSimulator = new InputSimulator();
10 office 27 }
28  
21 office 29 private InputSimulator InputSimulator { get; }
19 office 30  
10 office 31 private MqttCommunication MqttCommunication { get; }
32 private TaskScheduler TaskScheduler { get; }
33 private CancellationToken CancellationToken { get; }
34 private LocalKeyBindings LocalLocalKeyBindings { get; }
35  
36 public void Dispose()
37 {
38 MqttCommunication.OnMessageReceived -= OnMqttMessageReceived;
39 }
40  
41 public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting;
42  
35 office 43 private async void OnMqttMessageReceived(object sender, MqttCommunicationMessageReceivedEventArgs e)
10 office 44 {
35 office 45 if (e.Topic != "execute")
10 office 46 return;
47  
35 office 48 using (var memoryStream = new MemoryStream())
10 office 49 {
35 office 50 await e.PayloadStream.CopyToAsync(memoryStream);
51  
52 memoryStream.Position = 0L;
53  
10 office 54 var executeMouseKeyBinding =
55 (ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream);
56  
57 // Do not process own mouse key bindings.
58 if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal))
59 return;
60  
61 await Task.Delay(0, CancellationToken)
62 .ContinueWith(
63 _ => OnMouseKeyBindingExecuting?.Invoke(sender,
64 new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick,
65 executeMouseKeyBinding.Name)),
66 CancellationToken,
67 TaskContinuationOptions.None, TaskScheduler);
68  
21 office 69 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
70 Task.Run(() => Simulate(executeMouseKeyBinding), CancellationToken);
71 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
10 office 72 }
73 }
74  
20 office 75 private void Simulate(ExecuteKeyBinding executeBinding)
10 office 76 {
77 foreach (var localBinding in LocalLocalKeyBindings.Bindings)
78 {
79 if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal))
80 continue;
81  
24 office 82 // Skip any key bindings that are not enabled.
83 if (!localBinding.Enabled)
84 continue;
85  
22 office 86 // Key down
10 office 87 foreach (var key in localBinding.Keys)
88 {
21 office 89 if (!KeyConversion.StringToKeys.TryGetValue(key, out var press))
10 office 90 continue;
91  
22 office 92 InputSimulator.Keyboard.KeyDown((VirtualKeyCode) press);
19 office 93 }
22 office 94  
95 // Key up
96 foreach (var key in localBinding.Keys)
97 {
98 if (!KeyConversion.StringToKeys.TryGetValue(key, out var press))
99 continue;
100  
23 office 101 InputSimulator.Keyboard.KeyUp((VirtualKeyCode) press);
22 office 102 }
10 office 103 }
104 }
105 }
35 office 106 }