Install InputSimulator via NuGet
masterTo use the Windows Input Simulator in your .NET (C#) project, install the package using the NuGet Package Manager Console.
Install-Package InputSimulatorrepository·master·Indexed 21 days ago
https://github.com/michaelnoonan/inputsimulatorA C# wrapper for the Win32 SendInput method used to programmatically simulate keyboard and mouse input in WPF, Windows Forms, and Console applications. Provides functionality for single key presses, key down/up events, modified keystrokes, text entry, and querying the state of keys via VirtualKeyCode.
To use the Windows Input Simulator in your .NET (C#) project, install the package using the NuGet Package Manager Console.
Install-Package InputSimulatorYou can query the current state of the keyboard using the following methods:
IsKeyDown(VirtualKeyCode): Returns true if the specified key is currently pressed down.IsTogglingKeyInEffect(VirtualKeyCode): Returns true if a toggling key (like Caps Lock) is currently active/toggled on.public void GetKeyStatus()
{
// Determines if the shift key is currently down
var isShiftKeyDown = InputSimulator.IsKeyDown(VirtualKeyCode.SHIFT);
// Determines if the caps lock key is currently in effect (toggled on)
var isCapsLockOn = InputSimulator.IsTogglingKeyInEffect(VirtualKeyCode.CAPITAL);
}For more granular control, you can manually simulate the full lifecycle of a key press by calling SimulateKeyDown followed by SimulateKeyUp. This is useful for holding down modifier keys while pressing other keys.
public void ShoutHello()
{
// Simulate each key stroke
InputSimulator.SimulateKeyDown(VirtualKeyCode.SHIFT);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_H);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_E);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_L);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_L);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_O);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_1);
InputSimulator.SimulateKeyUp(VirtualKeyCode.SHIFT);
}Use InputSimulator.SimulateKeyPress to trigger a single key event using a VirtualKeyCode.
public void PressTheSpacebar()
{
InputSimulator.SimulateKeyPress(VirtualKeyCode.SPACE);
}The SimulateModifiedKeyStroke method allows you to simulate combinations of modifier keys (like CTRL, ALT, SHIFT) and standard keys. It supports single modifiers, arrays of modifiers, and arrays of keys to be pressed while those modifiers are held.
// CTRL-C
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
// CTRL-K-C (Simulated as CTRL-down, K, C, CTRL-up)
InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, new [] {VirtualKeyCode.VK_K, VirtualKeyCode.VK_C});
// Complex chord: CTRL-ALT-SHIFT-ESC-K
InputSimulator.SimulateModifiedKeyStroke(
new[] { VirtualKeyCode.CONTROL, VirtualKeyCode.MENU, VirtualKeyCode.SHIFT },
new[] { VirtualKeyCode.ESCAPE, VirtualKeyCode.VK_K });To quickly type a string of text, use SimulateTextEntry. This is a high-level abstraction that achieves the same result as individual key presses but is much simpler for typing sentences or words.
public void SayHello()
{
InputSimulator.SimulateTextEntry("Say hello!");
}