WingMan – Blame information for rev 20
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
10 | office | 1 | using System; |
19 | office | 2 | using System.Collections.Generic; |
10 | office | 3 | using System.IO; |
4 | using System.Threading; |
||
5 | using System.Threading.Tasks; |
||
19 | office | 6 | using WindowsInput; |
7 | using WindowsInput.Native; |
||
10 | office | 8 | using MQTTnet; |
9 | using SimWinInput; |
||
10 | using WingMan.Communication; |
||
14 | office | 11 | using WingMan.Utilities; |
10 | office | 12 | |
14 | office | 13 | namespace WingMan.Bindings |
10 | office | 14 | { |
15 | public class KeySimulator : IDisposable |
||
16 | { |
||
17 | public delegate void MouseKeyBindingExecuting(object sender, KeyBindingExecutingEventArgs args); |
||
18 | |||
19 | public KeySimulator(LocalKeyBindings localLocalKeyBindings, MqttCommunication mqttCommunication, |
||
20 | TaskScheduler formTaskScheduler, CancellationToken cancellationToken) |
||
21 | { |
||
22 | LocalLocalKeyBindings = localLocalKeyBindings; |
||
23 | MqttCommunication = mqttCommunication; |
||
24 | TaskScheduler = formTaskScheduler; |
||
25 | CancellationToken = cancellationToken; |
||
26 | |||
27 | MqttCommunication.OnMessageReceived += OnMqttMessageReceived; |
||
19 | office | 28 | |
29 | InputSimulator = new InputSimulator(); |
||
30 | /*var values = Enum.GetValues(typeof(InputSimulator)); |
||
31 | foreach (var i in values) |
||
32 | { |
||
33 | VirtualKeyCodeMapper.Add((int)i, values.GetValue()); |
||
34 | }*/ |
||
10 | office | 35 | } |
36 | |||
19 | office | 37 | private InputSimulator InputSimulator { get; set; } |
38 | |||
10 | office | 39 | private MqttCommunication MqttCommunication { get; } |
40 | private TaskScheduler TaskScheduler { get; } |
||
41 | private CancellationToken CancellationToken { get; } |
||
42 | private LocalKeyBindings LocalLocalKeyBindings { get; } |
||
43 | |||
44 | public void Dispose() |
||
45 | { |
||
46 | MqttCommunication.OnMessageReceived -= OnMqttMessageReceived; |
||
47 | } |
||
48 | |||
49 | public event MouseKeyBindingExecuting OnMouseKeyBindingExecuting; |
||
50 | |||
51 | private async void OnMqttMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e) |
||
52 | { |
||
53 | if (e.ApplicationMessage.Topic != "execute") |
||
54 | return; |
||
55 | |||
56 | using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) |
||
57 | { |
||
58 | var executeMouseKeyBinding = |
||
59 | (ExecuteKeyBinding) ExecuteKeyBinding.XmlSerializer.Deserialize(memoryStream); |
||
60 | |||
61 | // Do not process own mouse key bindings. |
||
62 | if (!string.Equals(executeMouseKeyBinding.Nick, MqttCommunication.Nick, StringComparison.Ordinal)) |
||
63 | return; |
||
64 | |||
65 | await Task.Delay(0, CancellationToken) |
||
66 | .ContinueWith( |
||
67 | _ => OnMouseKeyBindingExecuting?.Invoke(sender, |
||
68 | new KeyBindingExecutingEventArgs(executeMouseKeyBinding.Nick, |
||
69 | executeMouseKeyBinding.Name)), |
||
70 | CancellationToken, |
||
71 | TaskContinuationOptions.None, TaskScheduler); |
||
72 | |||
73 | Simulate(executeMouseKeyBinding); |
||
74 | } |
||
75 | } |
||
76 | |||
20 | office | 77 | private void Simulate(ExecuteKeyBinding executeBinding) |
10 | office | 78 | { |
79 | foreach (var localBinding in LocalLocalKeyBindings.Bindings) |
||
80 | { |
||
81 | if (!string.Equals(localBinding.Name, executeBinding.Name, StringComparison.Ordinal)) |
||
82 | continue; |
||
83 | |||
84 | foreach (var key in localBinding.Keys) |
||
85 | { |
||
14 | office | 86 | if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey)) |
10 | office | 87 | continue; |
88 | |||
19 | office | 89 | InputSimulator.Keyboard.KeyPress((VirtualKeyCode) pressKey); |
90 | } |
||
10 | office | 91 | } |
92 | } |
||
93 | } |
||
94 | } |