Language: EN

input-simulator

Emulate keyboard or mouse in .NET with Input Simulator

H.InputSimulator is a .NET library that allows you to simulate keyboard or mouse input under the Windows operating system.

In general, emulating input devices should be the last option for automation. But, in many cases, input automation is a very useful tool for integrating with other programs.

This library is based on the library https://github.com/TChatzigiannakis/InputSimulatorPlus, 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 input. Additionally, it can also simulate mouse movement, click simulation, or mouse wheel scrolling.

How to use InputSimulator

We can add the library to a .NET project easily, through the corresponding Nuget package.

Install-Package H.InputSimulator

The library provides the InputSimulator which has methods to emulate keyboard and mouse input in a “fluent” syntax.

For example, if we want to simulate the pressing of the Enter key, we can do so 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, write “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 input data automation. Its use is very simple and can save us a lot of time and effort in creating applications that require keyboard and mouse input simulation.

H.InputSimulator is OpenSource and the code is available at https://github.com/HavenDV/H.InputSimulator under the Microsoft Public License (Ms-PL) license.