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
20 changes: 20 additions & 0 deletions Sources/DesktopManager.Example/KeyboardInputExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Runtime.InteropServices;

namespace DesktopManager.Example;

/// <summary>
/// Demonstrates basic keyboard input using <see cref="KeyboardInputService"/>.
/// </summary>
internal static class KeyboardInputExample {
/// <summary>Runs the keyboard input example.</summary>
public static void Run() {
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
Console.WriteLine("Keyboard input examples require Windows.");
return;
}

Console.WriteLine("Pressing WIN+R to open Run dialog...");
KeyboardInputService.PressShortcut(VirtualKey.VK_LWIN, VirtualKey.VK_R);
}
}
3 changes: 3 additions & 0 deletions Sources/DesktopManager.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ static void Main(string[] args) {
// Demonstrate window keep-alive features
WindowKeepAliveExample.Run();

// Demonstrate keyboard input features
KeyboardInputExample.Run();

// Run monitor watcher example for 30 seconds
MonitorWatcherExample.RunAsync(TimeSpan.FromSeconds(30)).Wait();

Expand Down
33 changes: 33 additions & 0 deletions Sources/DesktopManager.Tests/KeyboardInputServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Runtime.InteropServices;

namespace DesktopManager.Tests;

[TestClass]
/// <summary>
/// Tests for <see cref="KeyboardInputService"/>.
/// </summary>
public class KeyboardInputServiceTests {
[TestMethod]
/// <summary>
/// Test for PressKey_DoesNotThrow.
/// </summary>
public void PressKey_DoesNotThrow() {
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
Assert.Inconclusive("Test requires Windows");
}

KeyboardInputService.PressKey(VirtualKey.VK_F24);
}

[TestMethod]
/// <summary>
/// Test for PressShortcut_DoesNotThrow.
/// </summary>
public void PressShortcut_DoesNotThrow() {
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
Assert.Inconclusive("Test requires Windows");
}

KeyboardInputService.PressShortcut(VirtualKey.VK_F23, VirtualKey.VK_F24);
}
}
2 changes: 2 additions & 0 deletions Sources/DesktopManager/DesktopManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

<ItemGroup>
<Compile Include="..\Shared\SupportedOSPlatformAttribute.cs" />
<Compile Update="KeyboardInputService.cs" />
<Compile Update="VirtualKey.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
74 changes: 74 additions & 0 deletions Sources/DesktopManager/KeyboardInputService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

namespace DesktopManager;

/// <summary>
/// Provides methods for simulating keyboard input.
/// </summary>
[SupportedOSPlatform("windows")]
public static class KeyboardInputService {
/// <summary>
/// Presses a single key using SendInput.
/// </summary>
/// <param name="key">Key to press.</param>
public static void PressKey(VirtualKey key) {
MonitorNativeMethods.INPUT[] inputs = new MonitorNativeMethods.INPUT[2];
inputs[0].Type = MonitorNativeMethods.INPUT_KEYBOARD;
inputs[0].Data.Keyboard = new MonitorNativeMethods.KEYBDINPUT {
Vk = (ushort)key,
Scan = 0,
Flags = 0,
Time = 0,
ExtraInfo = IntPtr.Zero
};
inputs[1].Type = MonitorNativeMethods.INPUT_KEYBOARD;
inputs[1].Data.Keyboard = new MonitorNativeMethods.KEYBDINPUT {
Vk = (ushort)key,
Scan = 0,
Flags = MonitorNativeMethods.KEYEVENTF_KEYUP,
Time = 0,
ExtraInfo = IntPtr.Zero
};
MonitorNativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<MonitorNativeMethods.INPUT>());
}

/// <summary>
/// Presses a shortcut combination of keys.
/// </summary>
/// <param name="keys">Keys to press in order.</param>
public static void PressShortcut(params VirtualKey[] keys) {
if (keys == null || keys.Length == 0) {
throw new ArgumentException("No keys specified", nameof(keys));
}

MonitorNativeMethods.INPUT[] inputs = new MonitorNativeMethods.INPUT[keys.Length * 2];
int index = 0;
foreach (VirtualKey key in keys) {
inputs[index].Type = MonitorNativeMethods.INPUT_KEYBOARD;
inputs[index].Data.Keyboard = new MonitorNativeMethods.KEYBDINPUT {
Vk = (ushort)key,
Scan = 0,
Flags = 0,
Time = 0,
ExtraInfo = IntPtr.Zero
};
index++;
}

for (int i = keys.Length - 1; i >= 0; i--) {
inputs[index].Type = MonitorNativeMethods.INPUT_KEYBOARD;
inputs[index].Data.Keyboard = new MonitorNativeMethods.KEYBDINPUT {
Vk = (ushort)keys[i],
Scan = 0,
Flags = MonitorNativeMethods.KEYEVENTF_KEYUP,
Time = 0,
ExtraInfo = IntPtr.Zero
};
index++;
}

MonitorNativeMethods.SendInput((uint)inputs.Length, inputs, Marshal.SizeOf<MonitorNativeMethods.INPUT>());
}
}
Loading
Loading