3
votes

I am using InputSimulator to simulate key presses and mouse clicks. Every key I've tested so far works, except the mouse buttons. I send them like this:

private void button2_Click(object sender, EventArgs e) //In this example I am trying to simulate the left mouse button
    {
        System.Threading.Thread.Sleep(2000); 
        InputSimulator.SimulateKeyPress(VirtualKeyCode.LBUTTON);
    }

But nothing happens. Do I do anything wrong?

Library: InputSimulator

2
Seems to be a known issue (maybe) LButton simulation is not working - LarsTech

2 Answers

6
votes

The latest version of InputSimulator supports mouse events. Here is how to use it:

var sim = new InputSimulator();
sim.Mouse.LeftButtonClick();

Note that binary download is outdated, so you have to build the library from sources.

3
votes

I dont know much about InputStimulator but according to this post. You can stimulate mouse click using;

      [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
      public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData,  int dwExtraInfo);

      private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
      private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
      private const int MOUSEEVENTF_LEFTUP = 0x0004;
      private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
      private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
      private const int MOUSEEVENTF_MOVE = 0x0001;
      private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
      private const int MOUSEEVENTF_RIGHTUP = 0x0010;
      private const int MOUSEEVENTF_WHEEL = 0x0800;
      private const int MOUSEEVENTF_XDOWN = 0x0080;
      private const int MOUSEEVENTF_XUP = 0x0100;

      //.................................
      //In your own function:

      int X = 1220;
      int Y = 13;
      mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, X, Y, 0, 0);
      mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);

Remember to add using System.Runtime.InteropService;