Skip to content

Commit

Permalink
Merge pull request #12 from royalapplications/feature-keyboardhooktog…
Browse files Browse the repository at this point in the history
…gleshortcut

Feature keyboardhooktoggleshortcut
  • Loading branch information
StefanKoell authored Jan 15, 2025
2 parents 825b6e4 + 820e73f commit 805fa68
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class InputConfiguration : ExpandableObjectConverter
/// </see>
public bool KeyboardHookMode { get; set; }

/// <summary>
/// When enabled, ctrl+alt+space can be used to toggle between local and remote keyboard hook modes. Only works when the session is in full screen mode.
/// </summary>
public bool KeyboardHookToggleShortcutEnabled { get; set; }

/// <summary>
/// Specifies the name of the active input locale identifier (formerly called the keyboard layout) to use for the connection.
/// </summary>
Expand Down
132 changes: 132 additions & 0 deletions src/RoyalApps.Community.Rdp.WinForms/Controls/RdpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,28 @@ public int KeyboardHookMode
set => SecuredSettings2.KeyboardHookMode = value;
}

/// <inheritdoc cref="KeyboardHookToggleShortcutEnabled"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool KeyboardHookToggleShortcutEnabled
{
get
{
if (!this.TryGetProperty<int>(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, out var value, out var ex))
Logger.LogWarning(ex, "Failed to get RDP client property: {PropertyName}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled);
return value == 1;
}
set
{
object keyboardHookToggleShortcutEnabled = value ? 1 : 0;
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, ref keyboardHookToggleShortcutEnabled, out var ex))
Logger.LogWarning(ex, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled, KeyboardHookToggleShortcutEnabled);

object keyboardHookToggleShortcutKey = "space";
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutKey, ref keyboardHookToggleShortcutKey, out var ex2))
Logger.LogWarning(ex2, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutKey, keyboardHookToggleShortcutKey);
}
}

/// <inheritdoc cref="KeyBoardLayoutStr"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string KeyBoardLayoutStr
Expand Down Expand Up @@ -1694,6 +1716,28 @@ public int KeyboardHookMode
set => SecuredSettings2.KeyboardHookMode = value;
}

/// <inheritdoc cref="KeyboardHookToggleShortcutEnabled"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool KeyboardHookToggleShortcutEnabled
{
get
{
if (!this.TryGetProperty<int>(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, out var value, out var ex))
Logger.LogWarning(ex, "Failed to get RDP client property: {PropertyName}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled);
return value == 1;
}
set
{
object keyboardHookToggleShortcutEnabled = value ? 1 : 0;
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, ref keyboardHookToggleShortcutEnabled, out var ex))
Logger.LogWarning(ex, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled, KeyboardHookToggleShortcutEnabled);

object keyboardHookToggleShortcutKey = "space";
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutKey, ref keyboardHookToggleShortcutKey, out var ex2))
Logger.LogWarning(ex2, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutKey, keyboardHookToggleShortcutKey);
}
}

/// <inheritdoc cref="KeyBoardLayoutStr"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string KeyBoardLayoutStr
Expand Down Expand Up @@ -2599,6 +2643,28 @@ public int KeyboardHookMode
set => SecuredSettings3.KeyboardHookMode = value;
}

/// <inheritdoc cref="KeyboardHookToggleShortcutEnabled"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool KeyboardHookToggleShortcutEnabled
{
get
{
if (!this.TryGetProperty<int>(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, out var value, out var ex))
Logger.LogWarning(ex, "Failed to get RDP client property: {PropertyName}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled);
return value == 1;
}
set
{
object keyboardHookToggleShortcutEnabled = value ? 1 : 0;
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, ref keyboardHookToggleShortcutEnabled, out var ex))
Logger.LogWarning(ex, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled, KeyboardHookToggleShortcutEnabled);

object keyboardHookToggleShortcutKey = "space";
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutKey, ref keyboardHookToggleShortcutKey, out var ex2))
Logger.LogWarning(ex2, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutKey, keyboardHookToggleShortcutKey);
}
}

/// <inheritdoc cref="KeyBoardLayoutStr"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string KeyBoardLayoutStr
Expand Down Expand Up @@ -3570,6 +3636,28 @@ public int KeyboardHookMode
set => SecuredSettings3.KeyboardHookMode = value;
}

/// <inheritdoc cref="KeyboardHookToggleShortcutEnabled"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool KeyboardHookToggleShortcutEnabled
{
get
{
if (!this.TryGetProperty<int>(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, out var value, out var ex))
Logger.LogWarning(ex, "Failed to get RDP client property: {PropertyName}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled);
return value == 1;
}
set
{
object keyboardHookToggleShortcutEnabled = value ? 1 : 0;
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, ref keyboardHookToggleShortcutEnabled, out var ex))
Logger.LogWarning(ex, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled, KeyboardHookToggleShortcutEnabled);

object keyboardHookToggleShortcutKey = "space";
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutKey, ref keyboardHookToggleShortcutKey, out var ex2))
Logger.LogWarning(ex2, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutKey, keyboardHookToggleShortcutKey);
}
}

/// <inheritdoc cref="KeyBoardLayoutStr"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string KeyBoardLayoutStr
Expand Down Expand Up @@ -4485,6 +4573,28 @@ public int KeyboardHookMode
set => SecuredSettings3.KeyboardHookMode = value;
}

/// <inheritdoc cref="KeyboardHookToggleShortcutEnabled"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool KeyboardHookToggleShortcutEnabled
{
get
{
if (!this.TryGetProperty<int>(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, out var value, out var ex))
Logger.LogWarning(ex, "Failed to get RDP client property: {PropertyName}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled);
return value == 1;
}
set
{
object keyboardHookToggleShortcutEnabled = value ? 1 : 0;
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, ref keyboardHookToggleShortcutEnabled, out var ex))
Logger.LogWarning(ex, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled, KeyboardHookToggleShortcutEnabled);

object keyboardHookToggleShortcutKey = "space";
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutKey, ref keyboardHookToggleShortcutKey, out var ex2))
Logger.LogWarning(ex2, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutKey, keyboardHookToggleShortcutKey);
}
}

/// <inheritdoc cref="KeyBoardLayoutStr"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string KeyBoardLayoutStr
Expand Down Expand Up @@ -5402,6 +5512,28 @@ public int KeyboardHookMode
set => SecuredSettings3.KeyboardHookMode = value;
}

/// <inheritdoc cref="KeyboardHookToggleShortcutEnabled"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool KeyboardHookToggleShortcutEnabled
{
get
{
if (!this.TryGetProperty<int>(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, out var value, out var ex))
Logger.LogWarning(ex, "Failed to get RDP client property: {PropertyName}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled);
return value == 1;
}
set
{
object keyboardHookToggleShortcutEnabled = value ? true : false;
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutEnabled, ref keyboardHookToggleShortcutEnabled, out var ex))
Logger.LogWarning(ex, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutEnabled, KeyboardHookToggleShortcutEnabled);

object keyboardHookToggleShortcutKey = "space";
if (!this.TrySetProperty(RdpClientExtensions.KeyboardHookToggleShortcutKey, ref keyboardHookToggleShortcutKey, out var ex2))
Logger.LogWarning(ex2, "Failed to set RDP client property: {PropertyName} to {PropertyValue}", RdpClientExtensions.KeyboardHookToggleShortcutKey, keyboardHookToggleShortcutKey);
}
}

/// <inheritdoc cref="KeyBoardLayoutStr"/>
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string KeyBoardLayoutStr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ namespace RoyalApps.Community.Rdp.WinForms.Controls;

internal static class RdpClientExtensions
{
public static readonly string DesktopScaleFactor = "DesktopScaleFactor";
public static readonly string DeviceScaleFactor = "DeviceScaleFactor";
public static readonly string DisableCredentialsDelegation = "DisableCredentialsDelegation";
public static readonly string DisableUdpTransport = "DisableUDPTransport";
public static readonly string EnableMouseJiggler = "EnableMouseJiggler";
public static readonly string EnableRdsAadAuth = "EnableRdsAadAuth";
public static readonly string EnableHardwareMode = "EnableHardwareMode";
public static readonly string MouseJigglerInterval = "MouseJigglerInterval";
public static readonly string MouseJigglerMethod = "MouseJigglerMethod";
public static readonly string PasswordContainsSmartcardPin = "PasswordContainsSCardPin";
public static readonly string RestrictedLogon = "RestrictedLogon";
public static readonly string RedirectedAuthentication = "RedirectedAuthentication";
public static readonly string ShowConnectionInformation = "ShowConnectionInformation";
public static readonly string EnableLocationRedirection = "EnableLocationRedirection";
public const string BaseProperties = "BaseProperties";
public const string CoreProperties = "CoreProperties";
public const string DesktopScaleFactor = "DesktopScaleFactor";
public const string DeviceScaleFactor = "DeviceScaleFactor";
public const string DisableCredentialsDelegation = "DisableCredentialsDelegation";
public const string DisableUdpTransport = "DisableUDPTransport";
public const string EnableMouseJiggler = "EnableMouseJiggler";
public const string EnableRdsAadAuth = "EnableRdsAadAuth";
public const string EnableHardwareMode = "EnableHardwareMode";
public const string MouseJigglerInterval = "MouseJigglerInterval";
public const string MouseJigglerMethod = "MouseJigglerMethod";
public const string KeyboardHookToggleShortcutEnabled = "KeyboardHookToggleShortcutEnabled";
public const string KeyboardHookToggleShortcutKey = "KeyboardHookToggleShortcutKey";
public const string PasswordContainsSmartcardPin = "PasswordContainsSCardPin";
public const string RestrictedLogon = "RestrictedLogon";
public const string RedirectedAuthentication = "RedirectedAuthentication";
public const string ShowConnectionInformation = "ShowConnectionInformation";
public const string EnableLocationRedirection = "EnableLocationRedirection";

/// <summary>
/// Applies the RdpClientConfiguration to the RdpClient.
Expand Down Expand Up @@ -113,6 +117,7 @@ public static void ApplyRdpClientConfiguration(this RdpControl rdpControl, RdpCl
rdpClient.LoadBalanceInfo = configuration.Connection.LoadBalanceInfo;
rdpClient.MaxReconnectAttempts = configuration.Connection.MaxReconnectAttempts;
rdpClient.UseRedirectionServerName = configuration.Connection.UseRedirectionServerName;
rdpClient.DisableUdpTransport = configuration.Connection.DisableUdpTransport;
rdpClient.KeepAliveInterval = configuration.Connection.ConnectionKeepAliveInterval.GetValueOrDefault() * 1000; //Convert seconds to milliseconds, use 0 for disabled
if (configuration.Connection.KeepAlive)
{
Expand Down Expand Up @@ -157,9 +162,11 @@ public static void ApplyRdpClientConfiguration(this RdpControl rdpControl, RdpCl
rdpClient.AcceleratorPassthrough = configuration.Input.AcceleratorPassthrough;
rdpClient.EnableWindowsKey = configuration.Input.EnableWindowsKey;
rdpClient.KeyboardHookMode = configuration.Input.KeyboardHookMode ? 1 : 0;
rdpClient.KeyboardHookToggleShortcutEnabled = configuration.Input.KeyboardHookToggleShortcutEnabled;

if (!string.IsNullOrWhiteSpace(configuration.Input.KeyBoardLayoutStr))
rdpClient.KeyBoardLayoutStr = configuration.Input.KeyBoardLayoutStr;

TraceConfigurationData(logger, configuration.Redirection);
rdpClient.AudioRedirectionMode = configuration.Redirection.AudioRedirectionMode;
rdpClient.AudioQualityMode = configuration.Redirection.AudioQualityMode;
Expand Down Expand Up @@ -316,12 +323,24 @@ public static bool TryGetProperty<T>(this IRdpClient rdpClient, string propertyN
value = default;
try
{
value = (T)rdpClient.GetExtendedSettings().get_Property(propertyName);
switch (propertyName)
{
case DisableUdpTransport:
value = (T)rdpClient.GetCoreProperties().get_Property(propertyName);
break;
default:
value = (T)rdpClient.GetExtendedSettings().get_Property(propertyName);
break;
}
}
catch (Exception ex)
{
exception = ex;
#if DEBUG
throw;
#else
return false;
#endif
}
return true;
}
Expand All @@ -342,12 +361,24 @@ public static bool TrySetProperty(this IRdpClient rdpClient, string propertyName
exception = null;
try
{
rdpClient.GetExtendedSettings().set_Property(propertyName, ref value);
switch (propertyName)
{
case DisableUdpTransport:
rdpClient.GetCoreProperties().set_Property(propertyName, ref value);
break;
default:
rdpClient.GetExtendedSettings().set_Property(propertyName, ref value);
break;
}
}
catch (Exception ex)
{
exception = ex;
#if DEBUG
throw;
#else
return false;
#endif
}
return true;
}
Expand Down Expand Up @@ -514,4 +545,14 @@ public static bool SetupCameraRedirection(this IRdpClient rdpClient, bool redire
exception = success ? null : new Exception(logMessage.ToString());
return success;
}

private static IMsRdpExtendedSettings GetCoreProperties(this IRdpClient rdpClient)
{
return (IMsRdpExtendedSettings)rdpClient.GetExtendedSettings().get_Property(CoreProperties);
}

private static IMsRdpExtendedSettings GetBaseProperties(this IRdpClient rdpClient)
{
return (IMsRdpExtendedSettings)rdpClient.GetExtendedSettings().get_Property(BaseProperties);
}
}
Loading

0 comments on commit 805fa68

Please sign in to comment.