Windows Input Simulator

repository·master·Indexed 21 days ago

https://github.com/michaelnoonan/inputsimulator

A C# wrapper for the Win32 SendInput method used to programmatically simulate keyboard and mouse input in WPF, Windows Forms, and Console applications. Provides functionality for single key presses, key down/up events, modified keystrokes, text entry, and querying the state of keys via VirtualKeyCode.

Tokens
1.1K
Snippets
6
Records
6
Agent score
24%

What's inside InputSimulator

  1. Check the state of keys

    master

    You can query the current state of the keyboard using the following methods:

    • IsKeyDown(VirtualKeyCode): Returns true if the specified key is currently pressed down.
    • IsTogglingKeyInEffect(VirtualKeyCode): Returns true if a toggling key (like Caps Lock) is currently active/toggled on.
    public void GetKeyStatus()
    {
      // Determines if the shift key is currently down
      var isShiftKeyDown = InputSimulator.IsKeyDown(VirtualKeyCode.SHIFT);
    
      // Determines if the caps lock key is currently in effect (toggled on)
      var isCapsLockOn = InputSimulator.IsTogglingKeyInEffect(VirtualKeyCode.CAPITAL);
    }
  2. Simulate key down and key up events

    master

    For more granular control, you can manually simulate the full lifecycle of a key press by calling SimulateKeyDown followed by SimulateKeyUp. This is useful for holding down modifier keys while pressing other keys.

    public void ShoutHello()
    {
      // Simulate each key stroke
      InputSimulator.SimulateKeyDown(VirtualKeyCode.SHIFT);
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_H);
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_E);
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_L);
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_L);
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_O);
      InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_1);
      InputSimulator.SimulateKeyUp(VirtualKeyCode.SHIFT);
    }
  3. Simulate modified keystrokes and chords

    master

    The SimulateModifiedKeyStroke method allows you to simulate combinations of modifier keys (like CTRL, ALT, SHIFT) and standard keys. It supports single modifiers, arrays of modifiers, and arrays of keys to be pressed while those modifiers are held.

    // CTRL-C
    InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
    
    // CTRL-K-C (Simulated as CTRL-down, K, C, CTRL-up)
    InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, new [] {VirtualKeyCode.VK_K, VirtualKeyCode.VK_C});
    
    // Complex chord: CTRL-ALT-SHIFT-ESC-K
    InputSimulator.SimulateModifiedKeyStroke(
      new[] { VirtualKeyCode.CONTROL, VirtualKeyCode.MENU, VirtualKeyCode.SHIFT },
      new[] { VirtualKeyCode.ESCAPE, VirtualKeyCode.VK_K });
  4. Simulate text entry

    master

    To quickly type a string of text, use SimulateTextEntry. This is a high-level abstraction that achieves the same result as individual key presses but is much simpler for typing sentences or words.

    public void SayHello()
    {
      InputSimulator.SimulateTextEntry("Say hello!");
    }