WingMan – Blame information for rev 32

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