InputSimulatorPro 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package InputSimulatorPro --version 1.0.1
                    
NuGet\Install-Package InputSimulatorPro -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InputSimulatorPro" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="InputSimulatorPro" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="InputSimulatorPro" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add InputSimulatorPro --version 1.0.1
                    
#r "nuget: InputSimulatorPro, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package InputSimulatorPro@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=InputSimulatorPro&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=InputSimulatorPro&version=1.0.1
                    
Install as a Cake Tool

Go to the docs on GitHub for better formating!

InputSimulatorPro

A wrapper for the User32.SendInput method that provides features unique to this package.

NuGet

NuGet

How to install: (Visual Studio)

  1. Right-click your project in the Solution Explorer
  2. Click 'Manage NuGet Packages...'
  3. Select the 'Browse' tab
  4. Enter 'InputSimulatorPro' in the search bar
  5. Select the result, click 'Install' and 'OK'
  6. Add a using statement at the top of your code

Dll

How to install: (Visual Studio)

  1. Download the InputSimulatorPro.dll-file

  2. In the solution explorer of your project, right-click the 'Dependencies'-Tab and click on 'Add Project Reference'

  3. Click on 'Browse' and locate the file

  4. Click 'OK'

  5. Add a using statement at tthe top of your code

    If you can, use the NuGet package for automated updates!

Changelog

v1.0.0 : Initial Release

v1.0.1 : Hotfix, structs and enums used for creating the INPUT struct were not public.

Docs

Introduction

The main class of this package is InputSimulator. It holds references to IKeyboard,IMouse,IKeyStateGrabber and IDebugger. Those hold the main features of the package.

IKeyboard

IKeyboard (or in the InputSimulator-class a Keyboard-instance) holds methods for keyboard related input simulations;

KeyDown:

public void KeyDown(VirtualKeyShort keyShort)

Takes in a VirtualKeyShort representing the key you want to simulate the KeyDown input for.

KeyUp:

public void KeyUp(VirtualKeyShort keyShort)

Takes in a VirtualKeyShort representing the key you want to simulate the KeyDown input for.

KeyPress:

public void KeyPress(VirtualKeyShort keyShort)

Takes in a VirtualKeyShort reporesenting the key you want to simulate a keypress (down and up input) for.

TextEntry:

public void TextEntry(string text)

Takes in a string representing the Text you want to enter. The method simulates the KeyDown and KeyUp input for every char in the string and mapps it to the VirtualKeyCode.

SimultaneousKeyPress:

public void SimultaneousKeyPress(VirtualKeyShort[] keyShorts)

Takes in an array of VirtualKeyShort representing the keys you want to simulate a keypress for at the same time. This can be used to simulate inputs that use the CTRL-key as a modifyer key.

SimultaneousKeyDown

public void SimultaneousKeyDown(VirtualKeyShort[] keyShorts)

Takes in an array of VirtualKeyShort representing the keys you want to simulate a key down input for at the same time. This can be used to simulate inputs that use the CTRL-key as a modifyer key.

SimultaneousKeyUp

public void SimultaneousKeyDown(VirtualKeyShort[] keyShorts)

Takes in an array of VirtualKeyShort representing the keys you want to simulate a key up input for at the same time. This can be used to simulate inputs that use the CTRL-key as a modifyer key.

Sleep

public void Sleep(int milliseconds)

Takes in an int that represents the timeout of the Thread in milliseconds. It utilizes Thread.Sleep and is just here for practical purposes. This mthod also has an overload which takes in a TimeSpan representing the timeout.

IMouse

IMouse (or in the InputSimulator-class a Mouse-instance) holds methods for mouse related input simulations;

KeyDown

public void KeyDown(MouseButton button)

Takes in a MouseButton representing the button you want simulate a down input for.

KeyUp

public void KeyUp(MouseButton button)

Takes in a MouseButton representing the button you want simulate a up input for.

KeyPress

public void KeyPress(MouseButton button)

Takes in a MouseButton representing the button you want to simulate a press input for (down and up input).

SetCursorPositionRelative

public void SetCursorPositionRelative(Vector2 coordinates)

Takes in a Vector2 representing the relative pixels amount you want to set the cursor position to. The (0,0) coordinate is always at the cursor (thats why it's relative movement).

SetCursorPositionAbsolute

public void SetCursorPositionAbsolute(Vector2 coordinates, bool useNormalizedCoordinates, bool virtualDesktop = false);

Takes in a Vector2 coordinates, bool useNormalizedCoordinates, bool virtualDesktop.

The coordinates represent the coordinates you want to set the cursors position to. If useNormaliedCoordinates is true, the coordinates are represented in pixels, if not the coordinates should be given in a range of 65535.0 to 0. useNormalizedCoordinates utilizes the GetSystemMetrics-method from the Windows-API and normalizes the coordinates in the scale of the MAIN monitor.

If virtualDesktop is true, it maps the coordinates to the whole virtual desktop. This is used for multi-monitor setups.

useNormalizedCoordinates and virtualDesktop can't be used together.

useNormalizedCoordinates may not be pixel-perfect due to floating point errors. (ca. 8p out of 10.000, no guarantee on that)

InterpolateCursorPositionRelative

public void InterpolateCursorPositionRelative(Vector2 coordinates, float t);

Takes in a Vector2 representing the coordinates you want interpolate the mouse position to, over a given period of time t in seconds. The start position is always the cursor (0,0). The coordinates are in pixels.

InterpolateCursorPositionAbsolute

public void InterpolateCursorPositionAbsolute(Vector2 startCoordinates, Vector2 endCoordinates, float t, bool useNormalizedCoordinates = true, bool virtualDesktop = false);

Takes in a Vector2 startCoordinates, Vector2 endCoordinates, float t, bool useNormalizedCoordinates, bool virtualDekstop.

The Vector2 startCoordinate represents the coordinates where the cursor position interpolation should be started, the endCoordinates the end. trepresents the time you want to interpolate the cursor position over in seconds.

If useNormalizedCoordinates is true, the coordinates are normalzed to the MAIN monitor using the GetSystemMetrics-method from the Windows-API. If not the coordinates should be given in a range of 65535.0 to 0.

If virtualDesktop is true the coordinates are mapped to the whole virtual desktop. This is used for multi-monitor setups.

useNormalizedCoordinates and virtualDesktop can't be used together.

useNormalizedCoordinates may not be pixel-perfect due to floating point errors. (ca. 8p out of 10.000, no guarantee on that)

VerticalWheel

public void VerticalWheel(int scroll)

Takes in a int representing the amount of vertical scroll. The scroll amount is multiplied by the default amount it takes to scroll one notch on a mouse (120)

HorizontalWheel

public void HorizontalWheel(int scroll)

Takes in a int representing the amount of horizontal scroll. The scroll amount is multiplied by the default amount it takes to scroll one notch on a mouse (120)

Sleep

public void Sleep(int milliseconds)

Takes in an int that represents the timeout of the Thread in milliseconds. It utilizes Thread.Sleep and is just here for practical purposes. This mthod also has an overload which takes in a TimeSpan representing the timeout.

IKeyStateGrabber

IKeyStateGrabber (or in the InputSimulator-class a KeyStateGrabber-instance) holds methods for checking the state of different keys;

IsVirtualKeyDown

public bool IsVirtualKeyDown(VirtualKeyShort key)

Takes in a VirtualKeyShort representing the key you want to check if the virtual key is down. Returns true if it is.

IsVirtualKeyUp

public bool IsVirtualKeyUp(VirtualKeyShort key)

Takes in a VirtualKeyShort representing the key you want to check if the virtual key is up. Returns true if it is.

IsHardwareKeyDown

public bool IsVirtualKeyDown(VirtualKeyShort key)

Takes in a VirtualKeyShort representing the key you want to check if the physical key is down. Returns true if it is.

IsHardwareKeyUp

public bool IsVirtualKeyUp(VirtualKeyShort key)

Takes in a VirtualKeyShort representing the key you want to check if the physical key is up. Returns true if it is.

IsToggleKeyInEffect

public bool IsToggleKeyInEffect(VirtualKeyShort key)

Takes in a VirtualKeyShort representing the key want to checck if it is in effect. This can be used to check wether a CTRL or SHIFTLOCK key is in effect. This can also be used to check if a regular key is in effect.

IDebugger

IDebugger (or in the InputSimulator-class a Debugger-instance) holds debug-related infos;

DoDebugInfo

public bool DoDebugInfo

The bool defines wether debug info should be printed to the console.

Version

public string Version

Returns the current Version you are using

Author

public string Author

Returns my GitHub name.

Log

public void Log(string message)

Logs a message to the console, if DoDebugInfo is true.

NativeMethods

The NativeMethods-class holds all the methods from the Windows-API used in this package, but also a few methods that can be useful while working with makros;

SetForegroundWindow

[DllImport("user32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd)

Takes in a IntPtr representing the window you want to set the foreground (focus) to.

FindWindows

[DllImport("user32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string? lpClassName, string lpWindowName)

Takes in two string, lpClassName representing the class name of the window you want to find and lpWindowName representing the exact window name of the window you want to find.

In my use-case lpClassName doesn't work too well, so I just pass in a null and the exact window name.

Returns an IntPtr representing the window.

Usage

You can use FindWindow and pass it into SetForegroundWindow to change the focus

Example

using InputSimulatorPro.Resources.Natives;

NativeMethods.SetForegroundWindow(NativeMethods.FindWindow(null , "Minecraft 1.8.9"));

How it works

The SendInput works by inserting inputs into the input stream of the device. These inputs are then executed like they were from a physical input and react with app-events, e.g. closes a tab in a browser when you press CTRL-W.

This also brings security flaws that are prevented like this:

This function is subject to UIPI. Applications are permitted to inject input only into applications that are at an equal or lesser integrity level.

Means that the code you are running has low or medium integrity and is not permitted to send inputs to higher integrity application (e.g. system).

Because the any inputs from the user or this application are but into an Input-Buffer that holds all inputs, user inputs can mess with the inputs send from the application.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 364 5/25/2024
1.0.3 277 9/29/2023
1.0.2 243 9/7/2023
1.0.1 213 9/6/2023
1.0.0 213 9/5/2023

Hotfix for structs and enums that are not visible but should be (basically everyone).