This is a fork of the Windows Input Simulator (WIS) project, ported from .NET Framework to .NET Standard 2.1 so it's compatible with .NET Core 5 & 6.
This project was created by michaelnoonan; I just ported it & fixed some outdated examples, which you can find below.
The Windows Input Simulator provides a simple programming interface to simulate keyboard & mouse input on Windows operating systems.
It supports keystrokes with modifier keys, and abstracts all of the required interop away so you can focus on your code.
Windows Forms provides the SendKeys method which can simulate text entry, but not actual key strokes. Windows Input Simulator can be used in WPF, Windows Forms and Console Applications to synthesize or simulate any Keyboard input including Control, Alt, Shift, Tab, Enter, Space, Backspace, the Windows Key, Caps Lock, Num Lock, Scroll Lock, Volume Up/Down and Mute, Web, Mail, Search, Favorites, Function Keys, Back and Forward navigation keys, Programmable keys and any other key defined in the Virtual Key table. It provides a simple API to simulate text entry, key down, key up, key press and complex modified key strokes and chords.
WIS uses the win32 SendInput
function via p/invoke to simulate keyboard, mouse, and hardware input.
You can install it via NuGet directly, or download the .nupkg
directly on the releases page.
Install via the commandline with:
Install-Package InputSimulatorEx
Everything is contained within the InputSimulatorEx
namespace.
using InputSimulatorEx;
public void PressTheSpacebar()
{
InputSimulator sim = new();
sim.Keyboard.KeyPress(VirtualKeyCode.SPACE);
}
public void ShoutHello()
{
InputSimulator sim = new();
// Simulate each key stroke
sim.Keyboard.KeyDown(VirtualKeyCode.SHIFT);
sim.Keyboard.KeyPress(VirtualKeyCode.VK_H);
sim.Keyboard.KeyPress(VirtualKeyCode.VK_E);
sim.Keyboard.KeyPress(VirtualKeyCode.VK_L);
sim.Keyboard.KeyPress(VirtualKeyCode.VK_L);
sim.Keyboard.KeyPress(VirtualKeyCode.VK_O);
sim.Keyboard.KeyPress(VirtualKeyCode.VK_1);
sim.Keyboard.KeyUp(VirtualKeyCode.SHIFT);
// Alternatively you can simulate text entry to acheive the same end result
sim.Keyboard.SimulateTextEntry("HELLO!");
}
public void SimulateSomeModifiedKeystrokes()
{
InputSimulator sim = new();
// CTRL-C (effectively a copy command in many situations)
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
// You can simulate chords with multiple modifiers
// For example CTRL-K-C whic is simulated as
// CTRL-down, K, C, CTRL-up
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, new [] {VirtualKeyCode.VK_K, VirtualKeyCode.VK_C});
// You can simulate complex chords with multiple modifiers and key presses
// For example CTRL-ALT-SHIFT-ESC-K which is simulated as
// CTRL-down, ALT-down, SHIFT-down, press ESC, press K, SHIFT-up, ALT-up, CTRL-up
sim.Keyboard.ModifiedKeyStroke(
new[] { VirtualKeyCode.CONTROL, VirtualKeyCode.MENU, VirtualKeyCode.SHIFT },
new[] { VirtualKeyCode.ESCAPE, VirtualKeyCode.VK_K });
}
public void SayHello()
{
InputSimulator sim = new();
sim.Keyboard.TextEntry("Say hello!");
}