radj307 / michaelnoonan.inputsimulator

Fork of Windows Input Simulator (C# SendInput Wrapper)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Windows Input Simulator

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.

What does it do?

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.

How does it work?

WIS uses the win32 SendInput function via p/invoke to simulate keyboard, mouse, and hardware input.

Usage

You can install it via NuGet directly, or download the .nupkg directly on the releases page.

NuGet

Install via the commandline with:
Install-Package InputSimulatorEx

Everything is contained within the InputSimulatorEx namespace.

using InputSimulatorEx;

Examples

Example: Single key press

public void PressTheSpacebar()
{
  InputSimulator sim = new();
  sim.Keyboard.KeyPress(VirtualKeyCode.SPACE);
}

Example: Key-down and Key-up

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!");
}

Example: Modified keystrokes such as CTRL-C

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 });
}

Example: Simulate text entry

public void SayHello()
{
  InputSimulator sim = new();
  sim.Keyboard.TextEntry("Say hello!");
}

About

Fork of Windows Input Simulator (C# SendInput Wrapper)

License:MIT License


Languages

Language:C# 98.0%Language:PowerShell 2.0%