WingMan – Blame information for rev 14
?pathlinks?
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 | |||
65 | private void Simulate(ExecuteKeyBinding executeBinding) |
||
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 | |||
81 | // Depress |
||
82 | foreach (var key in localBinding.Keys) |
||
83 | { |
||
14 | office | 84 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey)) |
10 | office | 85 | continue; |
86 | |||
14 | office | 87 | SimKeyboard.KeyUp(pressKey); |
10 | office | 88 | } |
89 | } |
||
90 | } |
||
91 | } |
||
92 | } |