WingMan – Diff between revs 14 and 18
?pathlinks?
Rev 14 | Rev 18 | |||
---|---|---|---|---|
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 MQTTnet; |
5 | using MQTTnet; |
|
6 | using SimWinInput; |
6 | using SimWinInput; |
|
7 | using WingMan.Communication; |
7 | using WingMan.Communication; |
|
8 | using WingMan.Utilities; |
8 | using WingMan.Utilities; |
|
9 | |
9 | |
|
10 | namespace WingMan.Bindings |
10 | namespace WingMan.Bindings |
|
11 | { |
11 | { |
|
12 | public class KeySimulator : IDisposable |
12 | public class KeySimulator : IDisposable |
|
13 | { |
13 | { |
|
14 | public delegate void MouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args); |
14 | public delegate void MouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args); |
|
15 | |
15 | |
|
16 | public KeySimulator(LocalKeyBindings localLocalKeyBindings, MqttCommunication mqttCommunication, |
16 | public KeySimulator(LocalKeyBindings localLocalKeyBindings, MqttCommunication mqttCommunication, |
|
17 | TaskScheduler formTaskScheduler, CancellationToken cancellationToken) |
17 | TaskScheduler formTaskScheduler, CancellationToken cancellationToken) |
|
18 | { |
18 | { |
|
19 | LocalLocalKeyBindings = localLocalKeyBindings; |
19 | LocalLocalKeyBindings = localLocalKeyBindings; |
|
20 | MqttCommunication = mqttCommunication; |
20 | MqttCommunication = mqttCommunication; |
|
21 | TaskScheduler = formTaskScheduler; |
21 | TaskScheduler = formTaskScheduler; |
|
22 | CancellationToken = cancellationToken; |
22 | CancellationToken = cancellationToken; |
|
23 | |
23 | |
|
24 | MqttCommunication.OnMessageReceived += OnMqttMessageReceived; |
24 | MqttCommunication.OnMessageReceived += OnMqttMessageReceived; |
|
25 | } |
25 | } |
|
26 | |
26 | |
|
27 | private MqttCommunication MqttCommunication { get; } |
27 | private MqttCommunication MqttCommunication { get; } |
|
28 | private TaskScheduler TaskScheduler { get; } |
28 | private TaskScheduler TaskScheduler { get; } |
|
29 | private CancellationToken CancellationToken { get; } |
29 | private CancellationToken CancellationToken { get; } |
|
30 | private LocalKeyBindings LocalLocalKeyBindings { get; } |
30 | private LocalKeyBindings LocalLocalKeyBindings { get; } |
|
31 | |
31 | |
|
32 | public void Dispose() |
32 | public void Dispose() |
|
33 | { |
33 | { |
|
34 | MqttCommunication.OnMessageReceived -= OnMqttMessageReceived; |
34 | MqttCommunication.OnMessageReceived -= OnMqttMessageReceived; |
|
35 | } |
35 | } |
|
36 | |
36 | |
|
37 | public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting; |
37 | public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting; |
|
38 | |
38 | |
|
39 | private async void OnMqttMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
39 | private async void OnMqttMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
|
40 | { |
40 | { |
|
41 | if (e.ApplicationMessage.Topic != "execute") |
41 | if (e.ApplicationMessage.Topic != "execute") |
|
42 | return; |
42 | return; |
|
43 | |
43 | |
|
44 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
44 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
|
45 | { |
45 | { |
|
46 | var executeMouseKeyBinding = |
46 | var executeMouseKeyBinding = |
|
47 | (ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream); |
47 | (ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream); |
|
48 | |
48 | |
|
49 | // Do not process own mouse key bindings. |
49 | // Do not process own mouse key bindings. |
|
50 | if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal)) |
50 | if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal)) |
|
51 | return; |
51 | return; |
|
52 | |
52 | |
|
53 | await Task.Delay(0, CancellationToken) |
53 | await Task.Delay(0, CancellationToken) |
|
54 | .ContinueWith( |
54 | .ContinueWith( |
|
55 | _ => OnMouseKeyBindingExecuting?.Invoke(sender, |
55 | _ => OnMouseKeyBindingExecuting?.Invoke(sender, |
|
56 | new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick, |
56 | new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick, |
|
57 | executeMouseKeyBinding.Name)), |
57 | executeMouseKeyBinding.Name)), |
|
58 | CancellationToken, |
58 | CancellationToken, |
|
59 | TaskContinuationOptions.None, TaskScheduler); |
59 | TaskContinuationOptions.None, TaskScheduler); |
|
60 | |
60 | |
|
61 | Simulate(executeMouseKeyBinding); |
61 | Simulate(executeMouseKeyBinding); |
|
62 | } |
62 | } |
|
63 | } |
63 | } |
|
64 | |
64 | |
|
65 | private void Simulate(ExecuteKeyBinding executeBinding) |
65 | private async void Simulate(ExecuteKeyBinding executeBinding) |
|
66 | { |
66 | { |
|
67 | foreach (var localBinding in LocalLocalKeyBindings.Bindings) |
67 | foreach (var localBinding in LocalLocalKeyBindings.Bindings) |
|
68 | { |
68 | { |
|
69 | if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal)) |
69 | if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal)) |
|
70 | continue; |
70 | continue; |
|
71 | |
71 | |
|
72 | // Press |
72 | // Press |
|
73 | foreach (var key in localBinding.Keys) |
73 | foreach (var key in localBinding.Keys) |
|
74 | { |
74 | { |
|
75 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey)) |
75 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey)) |
|
76 | continue; |
76 | continue; |
|
77 | |
77 | |
|
78 | SimKeyboard.KeyDown(pressKey); |
78 | SimKeyboard.KeyDown(pressKey); |
|
79 | } |
79 | } |
|
- | 80 | |
||
- | 81 | await Task.Delay(250); |
||
80 | |
82 | |
|
81 | // Depress |
83 | // Depress |
|
82 | foreach (var key in localBinding.Keys) |
84 | foreach (var key in localBinding.Keys) |
|
83 | { |
85 | { |
|
84 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey)) |
86 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey)) |
|
85 | continue; |
87 | continue; |
|
86 | |
88 | |
|
87 | SimKeyboard.KeyUp(pressKey); |
89 | SimKeyboard.KeyUp(pressKey); |
|
88 | } |
90 | } |
|
89 | } |
91 | } |
|
90 | } |
92 | } |
|
91 | } |
93 | } |
|
92 | } |
94 | } |
|
93 | |
95 | |