/trunk/WingMan/Bindings/KeyInterceptor.cs |
@@ -36,18 +36,13 @@ |
} |
|
private BatchBlock<string> KeyComboBatchBlock { get; set; } |
|
private ActionBlock<string[]> KeyComboActionBlock { get; set; } |
|
private IDisposable KeyComboDataFlowLink { get; set; } |
|
private SemaphoreSlim DataFlowSemaphoreSlim { get; } |
|
private RemoteKeyBindings RemoteKeyBindings { get; } |
private MqttCommunication MqttCommunication { get; } |
private TaskScheduler TaskScheduler { get; } |
private CancellationToken CancellationToken { get; } |
|
private IKeyboardMouseEvents MouseKeyGloalHook { get; set; } |
|
public void Dispose() |
@@ -75,6 +70,8 @@ |
|
// Create a sliding window of size equal to the longest key combination. |
var maxKeyComboLength = RemoteKeyBindings.Bindings.Max(binding => binding.Keys.Count); |
if (maxKeyComboLength <= 0) |
return; |
|
KeyComboBatchBlock = |
new BatchBlock<string>(maxKeyComboLength); |
@@ -139,7 +136,8 @@ |
await DataFlowSemaphoreSlim.WaitAsync(CancellationToken); |
try |
{ |
if (KeyComboBatchBlock != null) await KeyComboBatchBlock.SendAsync(key, CancellationToken); |
if (KeyComboBatchBlock != null) |
await KeyComboBatchBlock.SendAsync(key, CancellationToken); |
} |
finally |
{ |
/trunk/WingMan/Bindings/KeySimulator.cs |
@@ -1,5 +1,4 @@ |
using System; |
using System.Collections.Generic; |
using System.IO; |
using System.Threading; |
using System.Threading.Tasks; |
@@ -6,7 +5,6 @@ |
using WindowsInput; |
using WindowsInput.Native; |
using MQTTnet; |
using SimWinInput; |
using WingMan.Communication; |
using WingMan.Utilities; |
|
@@ -27,14 +25,9 @@ |
MqttCommunication.OnMessageReceived += OnMqttMessageReceived; |
|
InputSimulator = new InputSimulator(); |
/*var values = Enum.GetValues(typeof(InputSimulator)); |
foreach (var i in values) |
{ |
VirtualKeyCodeMapper.Add((int)i, values.GetValue()); |
}*/ |
} |
|
private InputSimulator InputSimulator { get; set; } |
private InputSimulator InputSimulator { get; } |
|
private MqttCommunication MqttCommunication { get; } |
private TaskScheduler TaskScheduler { get; } |
@@ -70,7 +63,9 @@ |
CancellationToken, |
TaskContinuationOptions.None, TaskScheduler); |
|
Simulate(executeMouseKeyBinding); |
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
Task.Run(() => Simulate(executeMouseKeyBinding), CancellationToken); |
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
} |
} |
|
@@ -83,10 +78,10 @@ |
|
foreach (var key in localBinding.Keys) |
{ |
if (!KeyConversion.StringToKeys.TryGetValue(key, out var pressKey)) |
if (!KeyConversion.StringToKeys.TryGetValue(key, out var press)) |
continue; |
|
InputSimulator.Keyboard.KeyPress((VirtualKeyCode) pressKey); |
InputSimulator.Keyboard.KeyPress((VirtualKeyCode) press); |
} |
} |
} |
/trunk/WingMan/WingMan.csproj |
@@ -49,12 +49,6 @@ |
<Reference Include="Open.Nat, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f22a6a4582336c76, processorArchitecture=MSIL"> |
<HintPath>..\packages\Open.NAT.2.1.0.0\lib\net45\Open.Nat.dll</HintPath> |
</Reference> |
<Reference Include="SimWinKeyboard, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL"> |
<HintPath>..\packages\SimWinKeyboard.1.0.2\lib\net20\SimWinKeyboard.dll</HintPath> |
</Reference> |
<Reference Include="SimWinMouse, Version=1.0.2.0, Culture=neutral, processorArchitecture=MSIL"> |
<HintPath>..\packages\SimWinMouse.1.0.2\lib\net20\SimWinMouse.dll</HintPath> |
</Reference> |
<Reference Include="System" /> |
<Reference Include="System.Core" /> |
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.5.24.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
/trunk/WingMan/WingManForm.Designer.cs |
@@ -274,7 +274,7 @@ |
// label6 |
// |
this.label6.AutoSize = true; |
this.label6.Location = new System.Drawing.Point(16, 80); |
this.label6.Location = new System.Drawing.Point(259, 20); |
this.label6.Name = "label6"; |
this.label6.Size = new System.Drawing.Size(53, 13); |
this.label6.TabIndex = 10; |
@@ -283,7 +283,7 @@ |
// Password |
// |
this.Password.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; |
this.Password.Location = new System.Drawing.Point(72, 76); |
this.Password.Location = new System.Drawing.Point(316, 16); |
this.Password.Name = "Password"; |
this.Password.Size = new System.Drawing.Size(160, 20); |
this.Password.TabIndex = 9; |
@@ -291,7 +291,7 @@ |
// label5 |
// |
this.label5.AutoSize = true; |
this.label5.Location = new System.Drawing.Point(16, 52); |
this.label5.Location = new System.Drawing.Point(112, 52); |
this.label5.Name = "label5"; |
this.label5.Size = new System.Drawing.Size(29, 13); |
this.label5.TabIndex = 8; |
@@ -300,7 +300,7 @@ |
// Nick |
// |
this.Nick.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; |
this.Nick.Location = new System.Drawing.Point(56, 48); |
this.Nick.Location = new System.Drawing.Point(152, 48); |
this.Nick.Name = "Nick"; |
this.Nick.Size = new System.Drawing.Size(176, 20); |
this.Nick.TabIndex = 7; |
@@ -308,7 +308,7 @@ |
// HostButton |
// |
this.HostButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; |
this.HostButton.Location = new System.Drawing.Point(368, 47); |
this.HostButton.Location = new System.Drawing.Point(248, 80); |
this.HostButton.Name = "HostButton"; |
this.HostButton.Size = new System.Drawing.Size(75, 23); |
this.HostButton.TabIndex = 6; |
@@ -319,7 +319,7 @@ |
// label4 |
// |
this.label4.AutoSize = true; |
this.label4.Location = new System.Drawing.Point(144, 20); |
this.label4.Location = new System.Drawing.Point(165, 20); |
this.label4.Name = "label4"; |
this.label4.Size = new System.Drawing.Size(26, 13); |
this.label4.TabIndex = 3; |
@@ -328,7 +328,7 @@ |
// ConnectButton |
// |
this.ConnectButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; |
this.ConnectButton.Location = new System.Drawing.Point(280, 47); |
this.ConnectButton.Location = new System.Drawing.Point(160, 80); |
this.ConnectButton.Name = "ConnectButton"; |
this.ConnectButton.Size = new System.Drawing.Size(75, 23); |
this.ConnectButton.TabIndex = 5; |
@@ -339,7 +339,7 @@ |
// Port |
// |
this.Port.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; |
this.Port.Location = new System.Drawing.Point(172, 16); |
this.Port.Location = new System.Drawing.Point(195, 16); |
this.Port.Name = "Port"; |
this.Port.Size = new System.Drawing.Size(60, 20); |
this.Port.TabIndex = 2; |
@@ -349,7 +349,7 @@ |
// label3 |
// |
this.label3.AutoSize = true; |
this.label3.Location = new System.Drawing.Point(8, 20); |
this.label3.Location = new System.Drawing.Point(32, 20); |
this.label3.Name = "label3"; |
this.label3.Size = new System.Drawing.Size(45, 13); |
this.label3.TabIndex = 1; |
@@ -358,7 +358,7 @@ |
// Address |
// |
this.Address.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; |
this.Address.Location = new System.Drawing.Point(56, 16); |
this.Address.Location = new System.Drawing.Point(81, 16); |
this.Address.Name = "Address"; |
this.Address.Size = new System.Drawing.Size(80, 20); |
this.Address.TabIndex = 0; |
/trunk/WingMan/WingManForm.cs |
@@ -112,6 +112,7 @@ |
|
public KeySimulator KeySimulator { get; set; } |
|
/// <inheritdoc /> |
/// <summary> |
/// Clean up any resources being used. |
/// </summary> |
@@ -243,7 +244,8 @@ |
replaceMouseBindings.Add(remoteBinding); |
} |
|
RemoteKeyBindings.Bindings = replaceMouseBindings; |
RemoteKeyBindings.Bindings.Clear(); |
foreach (var binding in replaceMouseBindings) RemoteKeyBindings.Bindings.Add(binding); |
|
RemoteBindingsListBox.Items.Clear(); |
RemoteBindingsListBox.DisplayMember = "Name"; |
@@ -287,7 +289,7 @@ |
return; |
} |
|
if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
if (!ValidateConnectionParameters(out var ipAddress, out var port, out var nick, out var password)) |
return; |
|
// Start the MQTT server. |
@@ -309,7 +311,11 @@ |
Password.Enabled = false; |
} |
|
private bool ValidateAddressPort(out IPAddress address, out int port, out string nick, out string password) |
private bool ValidateConnectionParameters( |
out IPAddress address, |
out int port, |
out string nick, |
out string password) |
{ |
address = IPAddress.Any; |
port = 0; |
@@ -329,11 +335,19 @@ |
} |
|
if (!IPAddress.TryParse(Address.Text, out address)) |
{ |
Address.BackColor = Color.LightPink; |
return false; |
} |
try |
{ |
address = Dns.GetHostAddresses(Address.Text).FirstOrDefault(); |
} |
catch (Exception ex) |
{ |
ActivityTextBox.AppendText( |
$"{Strings.Could_not_resolve_hostname} : {ex.Message}{Environment.NewLine}"); |
|
Address.BackColor = Color.LightPink; |
return false; |
} |
|
if (!uint.TryParse(Port.Text, out var uPort)) |
{ |
Port.BackColor = Color.LightPink; |
@@ -382,7 +396,7 @@ |
return; |
} |
|
if (!ValidateAddressPort(out var ipAddress, out var port, out var nick, out var password)) |
if (!ValidateConnectionParameters(out var ipAddress, out var port, out var nick, out var password)) |
return; |
|
if (!await MqttCommunication |
@@ -533,8 +547,8 @@ |
MouseKeyCombo = new List<string>(); |
|
MouseKeyApplicationHook = Hook.GlobalEvents(); |
MouseKeyApplicationHook.KeyUp += RemoteMouseKeyHookOnKeyUp; |
MouseKeyApplicationHook.KeyDown += RemoteMouseKeyHookOnKeyDown; |
MouseKeyApplicationHook.KeyUp += RemoteKeyHookOnKeyUp; |
MouseKeyApplicationHook.KeyDown += RemoteKeyHookOnKeyDown; |
} |
|
private void RemoteBindingsUnbindButtonClicked(object sender, EventArgs e) |
@@ -558,22 +572,23 @@ |
RemoteBindingsBindToBox.Text = string.Empty; |
} |
|
private void RemoteMouseKeyHookOnKeyDown(object sender, KeyEventArgs e) |
private void RemoteKeyHookOnKeyDown(object sender, KeyEventArgs e) |
{ |
e.SuppressKeyPress = true; |
|
KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key); |
if (!KeyConversion.KeysToString.TryGetValue((byte) e.KeyCode, out var key)) |
return; |
|
MouseKeyCombo.Add(key); |
} |
|
private async void RemoteMouseKeyHookOnKeyUp(object sender, KeyEventArgs e) |
private async void RemoteKeyHookOnKeyUp(object sender, KeyEventArgs e) |
{ |
RemoteKeyBindings.Bindings.Add(new RemoteKeyBinding(RemoteBindingsComboBox.Text, |
(string) RemoteBindingsListBox.SelectedItem, MouseKeyCombo)); |
|
MouseKeyApplicationHook.KeyDown -= RemoteMouseKeyHookOnKeyDown; |
MouseKeyApplicationHook.KeyUp -= RemoteMouseKeyHookOnKeyUp; |
MouseKeyApplicationHook.KeyDown -= RemoteKeyHookOnKeyDown; |
MouseKeyApplicationHook.KeyUp -= RemoteKeyHookOnKeyUp; |
|
MouseKeyApplicationHook.Dispose(); |
|
@@ -645,7 +660,8 @@ |
var loadedBindings = |
(LocalKeyBindings) LocalKeyBindings.XmlSerializer.Deserialize(memoryStream); |
|
foreach (var binding in loadedBindings.Bindings) LocalKeyBindings.Bindings.Add(binding); |
foreach (var binding in loadedBindings.Bindings) |
LocalKeyBindings.Bindings.Add(binding); |
|
LocalListBoxBindingSource.ResetBindings(false); |
} |
@@ -696,7 +712,8 @@ |
var loadedBindings = |
(RemoteKeyBindings) RemoteKeyBindings.XmlSerializer.Deserialize(memoryStream); |
|
foreach (var binding in loadedBindings.Bindings) RemoteKeyBindings.Bindings.Add(binding); |
foreach (var binding in loadedBindings.Bindings) |
RemoteKeyBindings.Bindings.Add(binding); |
} |
} |
} |