Search code examples
c#dllwindowdllimportinputsimulator

Can't access Windows Input Simulator in my C# project


After researching about how to control other applications by simulating keyboard, many people suggested that I use "inputsimulator" CodePlex

I unzipped the folder and looked for .dll file to reference in my C# project, but I couldn't find inputsimulator.dll file.

Here is what I have so far:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using WindowsInput;

    public class WindowHandling
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

    static void Main()
    {        
            InputSimulator.SimulateKeyDown(VirtualKeyCode.SHIFT);
    }
    }

And this is the error message:

'InputSimulator' does not contain a definition for 'SimulateKeyDown'

The name 'VirtualKeyCode' does not exist in the current context

How to reference InputSimulator?


Solution

  • The best way to bring this kind of dependency into your project is to use NuGet

    Install-Package InputSimulator -Version 1.0.4
    

    You can also use the "Manage NuGet Packages" option from the context menu on the project or solution file.

    NuGet will download the package, unzip it, and add the DLL to your project. It can also tell you when the package is updated.

    Also, the documentation you reference is way off. Here is an example.

    You'll need the using statement:

    using WindowsInput;
    

    And in your code, you need an instance:

    var inputSim = new InputSimulator();
    inputSim.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.RETURN);