WingMan – Blame information for rev 22
?pathlinks?
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 | |||
22 | office | 79 | // Key down |
10 | office | 80 | foreach (var key in localBinding.Keys) |
81 | { |
||
21 | office | 82 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var press)) |
10 | office | 83 | continue; |
84 | |||
22 | office | 85 | InputSimulator.Keyboard.KeyDown((VirtualKeyCode) press); |
19 | office | 86 | } |
22 | office | 87 | |
88 | // Key up |
||
89 | foreach (var key in localBinding.Keys) |
||
90 | { |
||
91 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var press)) |
||
92 | continue; |
||
93 | |||
94 | InputSimulator.Keyboard.KeyUp((VirtualKeyCode)press); |
||
95 | } |
||
10 | office | 96 | } |
97 | } |
||
98 | } |
||
99 | } |