H.InputSimulator is a library for .NET that allows simulating keyboard or mouse input on the Windows operating system.
In general, emulating input devices should be the last resort for automation. However, in many cases, input automation is a very useful tool for integrating with other programs.
This library is based on the https://github.com/TChatzigiannakis/InputSimulatorPlus library, which in turn is a fork of the well-known https://github.com/michaelnoonan/inputsimulator.
However, InputSimulator is not compatible with .NET Core, and InputSimulatorPlus is currently unmaintained.
InputSimulator provides functionalities for input simulation, such as simulating key presses, modified key presses, or direct text entry. Additionally, it can also simulate mouse movement, click simulation, or mouse wheel scrolling.
How to use InputSimulator
We can easily add the library to a .NET project via the corresponding Nuget package.
Install-Package H.InputSimulator
The library provides the InputSimulator class which contains methods for emulating keyboard and mouse input using a “fluent” syntax.
For example, if we want to simulate pressing the Enter key, we can do it with the following code:
using H.InputSimulator;
// ...
InputSimulator inputSimulator = new InputSimulator();
inputSimulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);
This code will simulate entering the phone number “555-555-5555” in the text field, then press the Tab key to move to the next text field, and finally, type “12345” in the second text field and press the Enter key to submit the form. Conclusion
inputSimulator.Keyboard.TextEntry("my text");
inputSimulator.Keyboard.KeyPress(VirtualKeyCode.TAB);
inputSimulator.Keyboard.TextEntry("12345");
inputSimulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);
Here are a couple of more complex examples, taken from the library’s documentation.
//SelfDestructMessage
new InputSimulator().Keyboard
.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_R)
.Sleep(1000)
.TextEntry("notepad")
.Sleep(1000)
.KeyPress(VirtualKeyCode.RETURN)
.Sleep(1000)
.TextEntry("These are your orders if you choose to accept them...")
.TextEntry("This message will self destruct in 5 seconds.")
.Sleep(5000)
.ModifiedKeyStroke(VirtualKeyCode.MENU, VirtualKeyCode.F4)
.KeyPress(VirtualKeyCode.VK_N);
//OpenPaintAndCreateLine
new InputSimulator().Keyboard
.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.VK_R)
.Sleep(1000)
.TextEntry("mspaint")
.Sleep(1000)
.KeyPress(VirtualKeyCode.RETURN)
.Sleep(1000)
.Mouse
.LeftButtonDown()
.MoveMouseToPositionOnVirtualDesktop(65535 / 2, 65535 / 2)
.LeftButtonUp();
As we can see, it is a very useful tool for automating data entry. Its use is very simple and can save us a lot of time and effort in creating applications that require simulating keyboard and mouse input.
H.InputSimulator is OpenSource and the code is available at https://github.com/HavenDV/H.InputSimulator under the Ms-PL Microsoft Public License (Ms-PL).

