Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/SwitchifyPc.Core/Input/DesktopCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,35 @@ private async Task<CommandExecutionResult> PressShortcutAsync(JsonElement keysEl
return CommandExecutionResult.Failure("unsafe_payload", "Shortcut key count is invalid.");
}

await adapter.PressShortcutAsync(keys, cancellationToken);
await PressShortcutRespectingHeldModifiersAsync(keys, cancellationToken);
return CommandExecutionResult.Success;
}

private async Task PressShortcutRespectingHeldModifiersAsync(IReadOnlyList<string> keys, CancellationToken cancellationToken)
{
List<string> temporaryKeys = [];
try
{
foreach (string key in keys)
{
if (activeModifierKeys.Contains(key))
{
continue;
}

await adapter.SetKeyDownAsync(key, true, cancellationToken).ConfigureAwait(false);
temporaryKeys.Add(key);
}
}
finally
{
foreach (string key in temporaryKeys.AsEnumerable().Reverse())
{
await adapter.SetKeyDownAsync(key, false, cancellationToken).ConfigureAwait(false);
}
}
}

private async Task<CommandExecutionResult> TypeTextAsync(string text, CancellationToken cancellationToken)
{
if (text.Length > ProtocolConstants.MaxTextLength)
Expand Down
2 changes: 1 addition & 1 deletion src/SwitchifyPc.Protocol/ProtocolConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static class ProtocolConstants
};

public static readonly IReadOnlySet<string> ShortcutKeys = new HashSet<string>(
KeyboardKeys.Concat(["Ctrl", "Alt", "Shift", "Meta", "A", "C", "V", "X", "Z", "Y"]),
KeyboardKeys.Concat(["Ctrl", "Alt", "Shift", "Meta"]).Concat(Enumerable.Range('A', 26).Select(code => ((char)code).ToString())),
StringComparer.Ordinal);

public static readonly IReadOnlySet<string> ModifierKeys = new HashSet<string>(StringComparer.Ordinal)
Expand Down
61 changes: 59 additions & 2 deletions src/SwitchifyPc.Tests/DesktopCommandExecutorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ public async Task MapsKeyboardTextMediaWindowAndPingCommands()
Assert.Equal(
[
"pressKey:Meta",
"pressShortcut:Meta",
"pressShortcut:Ctrl+C",
"setKeyDown:Meta:True",
"setKeyDown:Meta:False",
"setKeyDown:Ctrl:True",
"setKeyDown:C:True",
"setKeyDown:C:False",
"setKeyDown:Ctrl:False",
"typeText:Hello",
"mediaControl:playPause",
"controlWindow:switchNext"
Expand All @@ -61,6 +65,59 @@ public async Task MapsKeyboardTextMediaWindowAndPingCommands()
Assert.Equal(7, overlay.HideCount);
}

[Fact]
public async Task KeyboardShortcutsUseTemporaryKeyDownAndUp()
{
FakeInputAdapter adapter = new();
DesktopCommandExecutor executor = new(adapter);

await executor.ExecuteAsync(Command("keyboard.shortcut", new { keys = new[] { "Ctrl", "C" } }));

Assert.Equal(
[
"setKeyDown:Ctrl:True",
"setKeyDown:C:True",
"setKeyDown:C:False",
"setKeyDown:Ctrl:False"
],
adapter.Calls);
}

[Fact]
public async Task KeyboardShortcutsDoNotReleaseHeldModifiers()
{
FakeInputAdapter adapter = new();
FakeModifierOverlay modifierOverlay = new();
DesktopCommandExecutor executor = new(adapter, modifierOverlay: modifierOverlay);

await executor.ExecuteAsync(Command("keyboard.modifierDown", new { key = "Ctrl" }));
await executor.ExecuteAsync(Command("keyboard.shortcut", new { keys = new[] { "Ctrl", "C" } }));
await executor.ExecuteAsync(Command("keyboard.modifierDown", new { key = "Shift" }));
await executor.ExecuteAsync(Command("keyboard.shortcut", new { keys = new[] { "Ctrl", "Shift", "Z" } }));
await executor.ExecuteAsync(Command("keyboard.shortcut", new { keys = new[] { "Ctrl", "Alt", "F" } }));

Assert.Equal(
[
"setKeyDown:Ctrl:True",
"setKeyDown:C:True",
"setKeyDown:C:False",
"setKeyDown:Shift:True",
"setKeyDown:Z:True",
"setKeyDown:Z:False",
"setKeyDown:Alt:True",
"setKeyDown:F:True",
"setKeyDown:F:False",
"setKeyDown:Alt:False"
],
adapter.Calls);
Assert.Equal(["Ctrl", "Shift"], modifierOverlay.Changes.Last());

await executor.ReleaseHeldInputsAsync();

Assert.EndsWith("setKeyDown:Shift:False", adapter.Calls[^2]);
Assert.EndsWith("setKeyDown:Ctrl:False", adapter.Calls[^1]);
}

[Fact]
public async Task StreamsTextCharactersKeysAndChunks()
{
Expand Down
12 changes: 12 additions & 0 deletions src/SwitchifyPc.Tests/ProtocolConstantsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ public void PreservesCurrentProtocolLimits()
Assert.Equal(300, ProtocolConstants.MaxErrorMessageLength);
}

[Fact]
public void ShortcutKeysIncludeUppercaseAlphabetOnly()
{
foreach (string key in Enumerable.Range('A', 26).Select(code => ((char)code).ToString()))
{
Assert.Contains(key, ProtocolConstants.ShortcutKeys);
}

Assert.DoesNotContain("a", ProtocolConstants.ShortcutKeys);
Assert.Equal(6, ProtocolConstants.MaxShortcutKeys);
}

[Fact]
public void IncludesCurrentCommandTypes()
{
Expand Down
6 changes: 6 additions & 0 deletions src/SwitchifyPc.Tests/ProtocolValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public void AcceptsCurrentCommandPayloads()
new { type = "keyboard.modifierUp", payload = new { key = "Shift" } },
new { type = "keyboard.modifierUp", payload = new { key = "Meta" } },
new { type = "keyboard.shortcut", payload = new { keys = new[] { "Ctrl", "C" } } },
new { type = "keyboard.shortcut", payload = new { keys = new[] { "Ctrl", "A" } } },
new { type = "keyboard.shortcut", payload = new { keys = new[] { "Ctrl", "Shift", "Z" } } },
new { type = "keyboard.shortcut", payload = new { keys = new[] { "Alt", "F" } } },
new { type = "keyboard.shortcut", payload = new { keys = new[] { "Meta" } } },
new { type = "keyboard.typeText", payload = new { text = "Hello" } },
new { type = "keyboard.textStream.open", payload = new { streamId = "android-stream-1" } },
Expand Down Expand Up @@ -134,6 +137,9 @@ public void RejectsUnsafePayloads()
[
BaseCommand("mouse.move", new { dx = 501, dy = 0 }),
BaseCommand("keyboard.shortcut", new { keys = Array.Empty<string>() }),
BaseCommand("keyboard.shortcut", new { keys = new[] { "Ctrl", "a" } }),
BaseCommand("keyboard.shortcut", new { keys = new[] { "Ctrl", "1" } }),
BaseCommand("keyboard.shortcut", new { keys = new[] { "Ctrl", "." } }),
BaseCommand("keyboard.key", new { key = "F13" }),
BaseCommand("keyboard.key", new { key = "Win" }),
BaseCommand("keyboard.key", new { key = "Windows" }),
Expand Down
3 changes: 2 additions & 1 deletion src/SwitchifyPc.Tests/RemoteControlSessionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ public async Task DelegatesShortcutTextMediaAndWindowCommandsToCommandSession()
result => Assert.Equal("ble-1", Assert.Single(result.OutgoingMessages).ConnectionId));
Assert.Equal(
[
"pressShortcut:Meta",
"setKeyDown:Meta:True",
"setKeyDown:Meta:False",
"typeText:Hello",
"mediaControl:playPause",
"controlWindow:showDesktop"
Expand Down
28 changes: 28 additions & 0 deletions src/SwitchifyPc.Tests/WindowsDesktopInputAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,34 @@ public async Task PressesMetaAsWindowsKey()
Assert.Equal(["key:91:down", "key:91:up"], native.Calls);
}

[Fact]
public async Task MapsUppercaseShortcutLettersToVirtualKeys()
{
FakeNativeInput native = new();
WindowsDesktopInputAdapter adapter = new(native);

await adapter.PressShortcutAsync(["A", "M", "Z"]);

Assert.Equal(
[
"key:65:down",
"key:77:down",
"key:90:down",
"key:90:up",
"key:77:up",
"key:65:up"
],
native.Calls);
}

[Fact]
public async Task RejectsLowercaseShortcutLetters()
{
WindowsDesktopInputAdapter adapter = new(new FakeNativeInput());

await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => adapter.PressShortcutAsync(["a"]));
}

[Fact]
public async Task SetsModifierKeysDownAndUp()
{
Expand Down
7 changes: 6 additions & 1 deletion src/SwitchifyPc.Windows/Input/WindowsInputMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static ushort KeyboardVirtualKey(string key)
"Alt" => VkAlt,
"Shift" => VkShift,
"Meta" => VkLeftWindows,
"A" or "C" or "V" or "X" or "Y" or "Z" => key[0],
_ when IsUppercaseLetterKey(key) => key[0],
_ when IsFunctionKey(key, out ushort virtualKey) => virtualKey,
_ => throw new ArgumentOutOfRangeException(nameof(key), key, null)
};
Expand Down Expand Up @@ -106,4 +106,9 @@ private static bool IsFunctionKey(string key, out ushort virtualKey)
virtualKey = (ushort)(VkF1 + functionIndex - 1);
return true;
}

private static bool IsUppercaseLetterKey(string key)
{
return key.Length == 1 && key[0] is >= 'A' and <= 'Z';
}
}