WingMan – Diff between revs 23 and 24

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